Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > change the ID dynamically

Reply
Thread Tools

change the ID dynamically

 
 
mayurkoul2002@gmail.com
Guest
Posts: n/a
 
      05-31-2006
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

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-31-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-31-2006
Thomas 'PointedEars' Lahn wrote:

> (E-Mail Removed) 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

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-31-2006
Bart Van der Donck wrote:

> Thomas 'PointedEars' Lahn wrote:
>> (E-Mail 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.


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
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      05-31-2006
Thomas 'PointedEars' Lahn said the following on 5/31/2006 8:26 AM:
> Bart Van der Donck wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> (E-Mail 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';

>> 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/
 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      05-31-2006
Thomas 'PointedEars' Lahn wrote:

> Bart Van der Donck wrote:
>
> > Thomas 'PointedEars' Lahn wrote:
> >> (E-Mail 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

--
Bart

 
Reply With Quote
 
Noah Sussman
Guest
Posts: n/a
 
      06-01-2006
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.

(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
Inserting Controls Dynamically - with Validators Also Added Dynamically Jeffrey Todd ASP .Net 1 06-02-2005 04:33 PM
Load User Control Dynamically, Cast object dynamically =?Utf-8?B?UmV6YSBOYWJp?= ASP .Net 1 03-05-2005 12:04 AM
Load User Control Dynamically, Cast object dynamically =?Utf-8?B?UmV6YSBOYWJp?= ASP .Net 1 03-04-2005 07:57 PM



Advertisments