Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problem mit DOM

Empfohlene Antworten

Hallo zusammen,

ich spiele gerade ein bissel mit DOM - komme aber leider nicht weiter. Wäre nett wenn mir einer von euch weiterhelfen könnte.

Folgendes Problem:


<html><head>

</head>

<body>


<form>

<select name="Anzahl"id="anzahl" >

	<option value="">Anzahl wählen!</option>

	<option value="eins">Eins</option>

	<option value="zwei">Zwei</option>

</select>

</form>

<a href="" onClick="hinzufuegen()">Klick mich</a>


<SCRIPT type="text/javascript">

function hinzufuegen(){


	var knoten;

	var kind;

	knoten=document.getElementById("anzahl");

	kind=document.createElement("option");

	knoten.appendChild(kind);

	kind=document.createTextNode("Drei");

	knoten.appendChild(kind);

}

</SCRIPT>

</body>

</html>



Erstmal 2 Fragen:

- Warum fügt er mir die 3 nicht in meine Liste ein?

- Wenn ich statt knoten=document.getElementById("anzahl"); das ganze versuche per name zu bekommen, also knoten=document.getElementsByName("Anzahl") bekomme ich ein undefined zurück, warum?

Danke schonmal für die Hilfe!

Gruß,

DS

moin DarkSchlumpf,

Als erstes würde ich dem Forumlar einen Namen verpassen. Das sollte den Zugriff auf Formularelemente etwas vereinfachen.

<form name='auswahl'>...</form>
Nun nochmal kurz zum theoretischen Aufbau ...

  <document>

      <form>

          <option />

      </form>

  </document>

Dein Select-Element ist also dem Formular und nicht dem Document zugeordnet. Deshalb wahrscheinlich auch das undifined. Demnach solltest du mit

  document.[Formularname].Anzahl

auf das eigentliche Element zugreifen können.

- Warum fügt er mir die 3 nicht in meine Liste ein?

Ich hab die Erfahrung gemacht, dass du dein Objekt erst komplett aufbauen musst, und es dann erst als Child anhängen. Ausserdem müssen die Element Angaben in Großbuchstaben sein AFAIK:


var knoten;

var kind;


knoten=document.getElementById("anzahl");

kind=document.createElement("OPTION");	

kind.appendChild(document.createTextNode("Drei"));        

knoten.appendChild(kind);

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.