Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question how to select multiple p or div with same name or id?

    I'm using this function to select one paragraph and change the size of the font on it. How can I modify the function so it will select all paragraphs and divs with the same id or name?

    function changeFontSize(fontSize,id) {
    newFontSize = parseInt(fontSize);
    document.getElementById(id).style.fontSize = newFontSize + "%";
    }

    <a href="javascript:changeFontSize(70,target);">small</a>
    <a href="javascript:changeFontSize(100,target);">big</a>

    <p id="target">bla</p>

    <div>foo</div>

    <div id="target">bla</div>

    <p>foo</p>

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    It is invalid to assign the same id to multiple elements. Instead, maybe you should use a class and some DOM2 Stylesheets:

    pretend ".target" is the first rule in your first stylesheet, and that you give the elements class="target":

    (document.styleSheets[0].cssRules || document.styleSheets[0].rules)[0].style.fontSize = newFontSize + "%";

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks jkd, any other alternatives without making use of css? probably using names instead of ids?

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by jkd
    It is invalid to assign the same id to multiple elements.
    He's right. You need to pick some new names for your ID's.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    probably using names instead of ids
    It is not the same thing. ID reference is more strictly than name ref.

    There are cases when you may have the same name for different elements (for instance check boxes or radio buttons) but you may not use the same ID for different objects. Thus, the browser will ignore all, but the last ID reference, that's all.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •