467,162 Members | 934 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,162 developers. It's quick & easy.

change the ID dynamically

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

May 31 '06 #1
  • viewed: 47060
Share:
6 Replies
ma***********@gmail.com wrote:
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


That is as impossible as your posting "style".

<URL:http://jibbering.com/faq/>
PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
May 31 '06 #2
Thomas 'PointedEars' Lahn wrote:
ma***********@gmail.com wrote:
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


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>

--
Bart

May 31 '06 #3
Bart Van der Donck wrote:
Thomas 'PointedEars' Lahn wrote:
ma***********@gmail.com 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.
PointedEars
--
In the First World War, 13 million people were killed. In the Second
World War, 40 million people were killed. I think that if a third war
takes place, nothing is going to be left on the face of earth.
-- Shakira, 2003-02-05 @ MTV.com
May 31 '06 #4
Thomas 'PointedEars' Lahn said the following on 5/31/2006 8:26 AM:
Bart Van der Donck wrote:
Thomas 'PointedEars' Lahn wrote:
ma***********@gmail.com 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';
alert(document.getElementById('myID').innerHTML);
</script>
</body>
</html>
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 31 '06 #5
Thomas 'PointedEars' Lahn wrote:
Bart Van der Donck wrote:
Thomas 'PointedEars' Lahn wrote:
ma***********@gmail.com 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 :-)

--
Bart

May 31 '06 #6
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[i].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.

ma***********@gmail.com wrote:
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


Jun 1 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by puunda@gmail.com | last post: by
3 posts views Thread by Quentin | last post: by
37 posts views Thread by sam44 | last post: by
3 posts views Thread by DMA | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.