Jump to content

What does this mean?


Thunder
 Share

Recommended Posts

It will get an element that has the speicified ID attribute.Here's an example of how it's used:<html><head><title>Test document</title><script type="text/javascript">function changeText() {document.getElementById("something").firstChild.nodeValue = "New text";}</script></head><body><h1>Test document</h1><div id="something">This text will be changed by Javascript</div><button onclick="changetext()">Click here</button><p>Some text</p></body></html>

Link to comment
Share on other sites

When you get an element in Javascript you have access to the properties and methods for that object. Assume that you have an input element with a certain ID, like a text field:<input type="text" id="input1" size="10" maxlength="20" value="enter text">You can get the element that represents that text field and change whatever you want about it. You can set the value:document.getElementById("input1").value = "new value";You can change something like the size:document.getElementById("input1").setAttribute("size", 50);You can even make it a different type:document.getElementById("input1").setAttribute("type", "checkbox");There are some issues with setting the type but you should get the idea, when you have access to the element you can change how it looks or behaves. You can set an onclick handler for example:document.getElementById("input1").addEventListener("click", function () { alert("you clicked the input"); }, false);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...