Jump to content
Sign in to follow this  

Setting Element's Id Using Html Dom

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?????!?!

Share this post

Link to post
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)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...