user3169945
user3169945

Reputation: 13

Javascript Function is not running

it seems to be simple: i have this Form with the Javascript Code. the "-" Button removes an input set and the "+" Button(at the bottom of the Code) should add one, but it doesnt work? the Script is originaly from http://www.quirksmode.org/dom/domform.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Formular</title>

<script type="text/javascript">
<!--
function init() {
document.getElementById('moreFields').onclick = moreFields;
moreFields();
}
var counter = 0;
function moreFields() {
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
window.onload = moreFields;
-->
</script>
</head>
<body>
<a href="/">Home</a> | <a href="/new/">Neue Regel</a><br><hr><br>
<div id="readroot" style="display: none">
<p>Adresse: <input name="c_adddress" type="text">
Value: <input name="c_value" type="text">
<input type="button" value=" - " onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /></p></div>
<form action="/save/">
<p>Name:<input name="name" type="text"></p>
<p><b>Befehl:</b><br>
<p>Adresse: <input name="b_adddress" type="text"> 
Value: <input name="b_value" type="text"></p>
<p><b>Bedingungen</b></p>
<span id="writeroot"></span>
<input type="button"id="moreFields" value=" + " />
<p><input type="submit" value="speichern">
<input type="reset" value="zur&uuml;cksetzen"></p>
</form>
</body>
</html>

Upvotes: 0

Views: 108

Answers (1)

Saravana
Saravana

Reputation: 40534

The onclick event is not registered for your moreFields button. And you haven't called the init function anywhere.

Change the following lines:

document.getElementById('moreFields').onclick = moreFields; // Notice no ()
...
window.onload = init; // This will make sure the above code is run after the DOM is loaded

Upvotes: 2

Related Questions