change the ID dynamically

M

mayurkoul2002

hello there.....
i have a problem here...........i want to assign ID attribute
dynamically to the html tags......and i need a function for tht....can
anybody help me out in tht
 
B

Bart Van der Donck

Thomas said:
That is as impossible as your posting "style".

Why would that be impossible ?

<html>
<body>
<p>content</p>
<script language="javascript" type="text/javascript">
document.getElementsByTagName('p')[0].setAttribute('id','myID');
alert(document.getElementById('myID').innerHTML);
</script>
</body>
</html>
 
T

Thomas 'PointedEars' Lahn

Bart said:
Thomas said:
[assign ID attribute dynamically to the html tags]

That is as impossible as your posting "style".

Why would that be impossible ?

<html>
<body>
<p>content</p>
<script language="javascript" type="text/javascript">

The `language' attribute is deprecated long since, and your markup
is not Valid; especially, the missing DOCTYPE declaration triggers
Quirks Mode, which can change DOM behavior.
document.getElementsByTagName('p')[0].setAttribute('id','myID');
alert(document.getElementById('myID').innerHTML);
</script>
</body>
</html>

That is setting the attribute, not changing it. Note the Subject header.
Furthermore:

- This requires other DOM methods that may not be available.

- It is known to be not possible for some elements in some DOMs
(IIRC there are issues in the IE DOM).

- setAttribute() implementations are known to be buggy.


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 5/31/2006 8:26 AM:
Bart said:
Thomas said:
(e-mail address removed) wrote:
[assign ID attribute dynamically to the html tags]
That is as impossible as your posting "style".
Why would that be impossible ?

<html>
<body>
<p>content</p>
<script language="javascript" type="text/javascript">

The `language' attribute is deprecated long since, and your markup
is not Valid;

Semi-valid point but irrelevant to the rebuttal to your assertion that
it was impossible.
especially, the missing DOCTYPE declaration triggers Quirks Mode,
which can change DOM behavior.

Only in IE and in this case it makes no difference.
document.getElementsByTagName('p')[0].setAttribute('id','myID');

document.getElementById('p')[0].id = 'newID';
That is setting the attribute, not changing it. Note the Subject header.

Yoohoo, dimwit, think about it. An element has an ID. You "set" the ID,
then alert that elements ID. It will give you the new ID. That is
changing the ID. You can be as pedantic as you want but the ID got changed.
Furthermore:

- This requires other DOM methods that may not be available.

Only if the user is using an antiquated anti-social browser.
- It is known to be not possible for some elements in some DOMs
(IIRC there are issues in the IE DOM).

Such as?
- setAttribute() implementations are known to be buggy.

Then you offer a better alternative. Or, preferably, you STFU and move on.
 
B

Bart Van der Donck

Thomas said:
Bart said:
Thomas said:
(e-mail address removed) wrote:
[assign ID attribute dynamically to the html tags]

That is as impossible as your posting "style".

Why would that be impossible ?

<html>
<body>
<p>content</p>
<script language="javascript" type="text/javascript">

The `language' attribute is deprecated long since, and your markup
is not Valid; especially, the missing DOCTYPE declaration triggers
Quirks Mode, which can change DOM behavior.
document.getElementsByTagName('p')[0].setAttribute('id','myID');
alert(document.getElementById('myID').innerHTML);
</script>
</body>
</html>

That is setting the attribute, not changing it. Note the Subject header.
Furthermore:

- This requires other DOM methods that may not be available.

- It is known to be not possible for some elements in some DOMs
(IIRC there are issues in the IE DOM).

- setAttribute() implementations are known to be buggy.

Hmmmm, that Asperger-smell in the morning :)
 
N

Noah Sussman

You can assign an id to any element simply by saying:

El.id = "foo";

If, for instance, you wanted to assign an ID to every FORM tag on the
page, you could do:

for(var i=0; i<document.forms.length; i++) {
document.forms.id = "formtag_" + i;
}

If you had three FORM tags on your page and you ran that loop, the form
tags would have ids "formtag_0", "formtag_1", "formtag_2".

Exactly how you will iterate over the tags you want to assign IDs to
will depend on exactly which tags you need to work with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top