View Full Version : how to select multiple p or div with same name or id?

12-21-2003, 07:50 PM
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 id="target">bla</div>


12-21-2003, 07:59 PM
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 + "%";

12-22-2003, 07:36 PM
Thanks jkd, any other alternatives without making use of css? probably using names instead of ids? :thumbsup:

12-22-2003, 10:40 PM
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.

12-23-2003, 09:22 AM
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.