Beruflich Dokumente
Kultur Dokumente
The input text boxes in a form have the special name value, and contain string
values. Convert these to integers using parseInt().
var v1 = parseInt(document.myform.val1.value);
var v2 = parseInt(document.myform.val2.value);
document.getElementById("two").appendChild(textnode);
Then we store the new element into its container (a <form> element in this case).
sets the element text of element "two" to that specified by textnode.
node=document.getElementById("myform");
Bringing these together, using variables to store intermediate values: node.appendChild(el);
node = document.getElementById("xx");
newText = document.createTextNode ("Hello World");
node.appendChild(newText);
Identifying, Creating, Deleting and modifying HTML elements - 3 Identifying, Creating, Deleting and modifying HTML elements - 4
We can remove HTML elements that are referenced using the hierarchic notation: If we have multiple text nodes stored in a node, we can reference them in the array
node.childNodes[], e.g.
node=document.getElementById("myform");
node = document.getElementById("msgs");
while (node.childNodes.length > 0)
node.removeChild (node.in0);
{ node.removeChild (node.childNodes[0]);
}
Alternatively, we can identify an input node by ID. Notice that the node from which
We could also use while (node.hasChildNodes()) above.
the child is removed must be its immediate parent.
el = document.getElementById("in1");
node.removeChild (el);
We can also replace element text directly:
node = document.getElementById("msg");
newText = document.createTextNode ( "Hello" );
node.replaceChild(newText, node.childNodes[0]);
Notice that these programs (including extend.html) perform no error checking that
the elements exist before they try to delete them.
Beware that getElementsByTagName("*") does not return text nodes - use We set its attributes separately:
<span> around the text to make it accessible. el.setAttribute ("x", 400);
el.setAttribute ("y", 100);
el.setAttribute ("style", "font-size: "+fontSize);
Then we inser t the textnode into the text element and store the new element into its
container (a <g> element in this case).
el.appendChild(newText);
node = document.getElementById("myGroup");
node.appendChild(el);
...
<form action="http://www.computing.surrey.ac.uk/cgi-bin/R.Peel/getpost.pl"
method="post" onsubmit="return verify()">
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
function over() {
this.style.backgroundColor=’#cc0000’
}
function out() {
this.style.backgroundColor=’#ffffff’
}
Don’t use x[i].onmouseover = over(); above - you will run the over()
function before installing its result in the onmouseover location within x[i].
Without the brackets, you install a reference to the function that only runs when the
mouse event takes place.