How to add HTML controls / elements dynamically to a page ------------------------------------------------------------------------------------------------ Hmm .... now there's a familiar javascript problem. You know what javascript is ... know how it works ... know that you can manipulate controls, but how do you actually add new elements to the page using JS. To revise our principles and beliefs -- The javascript hierarchy looks like this : Document --> HTML --> Body --> Page Elements. The above expression omits one essential fact though - "Controls on a page can be children of other controls on the page". Confused ? Let's look at an example :
<html>
<head>
<title>Untitled Page</title>
</head>
<body>
<div id="myDiv">
<input type="button" id="btn" value="Click Me" />
</div>
</body>
</html>
In the above html, the <input> element with id = "btn", is a CHILD OF the <div> element.Getting the hang of it ??!
Let's fudge up things a little bit <html> <head> <title>Untitled Page</title> </head> <body> <div id="myDiv"> <input type="button" id="btn" value="Click Me" /> <div id="childDiv"> <h id="childH"> Child </h> </div> </div> </body> </html>Again, the hierarchy we have is :

<html> <head> <title>Untitled Page</title> </head> <body> <div id="myDiv"> <input type="button" id="btn" value="Click Me" /> <div id="childDiv"> <h id="childH"> Child </h> <input type="button" id="addCtrl" value="Add a new Elt" onclick="var elt = document.getElementById('childDiv'); var newElt = document.createElement('LABEL'); newElt.innerText = 'I was just added'; elt.appendChild(newElt);" /> </div> </div> </body> </html>Ok, so here we go. First, look at the line beginning with "onclick". This is like telling the browser, "Whenever the user does a click on this button (that is, clicks the button), run this piece of javascript". Simple, huh. And lets dissect the javascript -- var elt = document.getElementById('childDiv'); You should be familiar with this. If not, look at my post on javascript debugging. This is fetching the object for childDiv, and assigning to the variable 'elt'. Next up, var newElt = document.createElement('LABEL'); Here, we are creating a new element of type 'label'. Usually, when you need to display some plain text on the screen, we use this type of element. The new element is now stored in the 'newElt' variable. Next, newElt.innerText = 'I was just added'; This is basically setting a property for that control, so that when the object is shown on the browser, it writes "I was just added" on screen, and we know that it exists. elt.appendChild(newElt); Now this is the most critical part. We are adding the element we created above using the 'createElement' method, to the element that elt refers to. Meaning, we are adding the newly created element 'newElt' to the 'childDiv' element. Once done, the hierarchy would look like this :

2 comments:
when you call .createElement('label');
be sure to use lower case tag names.
Although it will still work in HTML4, in XHTML, this will cause issues.
Besides, it looks very amaturish and reminicent of the old, and bad code samples on MSDN
First of all, thank you for your comments. The post was meant for the amateur learner, and our aim is to gradually advance the reader to more complex topics like OO javascript, ajax etc. Once again, thank you and stay tuned.
Post a Comment