Jump to content

Setting Element's Id Using Html Dom


misse

Recommended Posts

hello everyone out there!!!am a student in 4th in computer science, i'm having a forum project in 'web application' subject. my question is, while creating elements dynamically, is it possible to set their ID's as well as setting their attributes using HTML DOM?????!?!

Link to comment
Share on other sites

Yes. Before inserting the new element, keep a reference to it, and set the desired attributes as you would on an existing element. For example:

var newElement = document.createElement("div");newElement.setAttribute("id", "newElementId");

or (replace the second line with)

newElement.id = "newElementId";

both are currently equivalent of

<div id="newElementId"></div>

and this is what is going to get inserted if you insert this element somewhere.BTW, by "creating elements dynamically", you do mean with JavaScript, right? HTML DOM (the second form) can't be used in most DOM environments (PHP, just to name one), but the first one, XML DOM, can, only with the specific language's syntax rules in mind of course. For example, in PHP, the equivalent of the above would be:

$dom = new DOMDocument;$newElement = $dom->createElement("div");$newElement->setAttribute("id", "newElementId");

(again, the second form won't work in PHP)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...