change the ID dynamically

Discussion in 'Javascript' started by mayurkoul2002@gmail.com, May 31, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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
    Thomas 'PointedEars' Lahn, May 31, 2006
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn wrote:

    > 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
    Bart Van der Donck, May 31, 2006
    #3
  4. Bart Van der Donck wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> 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
    Thomas 'PointedEars' Lahn, May 31, 2006
    #4
  5. Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 5/31/2006 8:26 AM:
    > Bart Van der Donck wrote:
    >
    >> Thomas 'PointedEars' Lahn wrote:
    >>> 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/
    Randy Webb, May 31, 2006
    #5
  6. Thomas 'PointedEars' Lahn wrote:

    > Bart Van der Donck wrote:
    >
    > > Thomas 'PointedEars' Lahn wrote:
    > >> 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
    Bart Van der Donck, May 31, 2006
    #6
  7. Noah Sussman Guest

    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.

    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
    Noah Sussman, Jun 1, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    6
    Views:
    985
    VB Programmer
    Jul 14, 2003
  2. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,721
    Matt Berther
    Mar 5, 2005
  3. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,354
    Scott Allen
    Mar 4, 2005
  4. Jeffrey Todd
    Replies:
    1
    Views:
    4,103
    Peter Blum
    Jun 2, 2005
  5. msimmons
    Replies:
    0
    Views:
    472
    msimmons
    Jul 16, 2009
Loading...

Share This Page