0

everybody! I want to do following: When clicked on check box one or more div tags must change their css-style. I have this little javascript:

function changeStyle(o) {
    if(o.checked) {
        document.getElementById(o.getAttribute("value")).setAttribute('class','on');
    }
    else {
        document.getElementById(o.getAttribute("value")).setAttribute('class','off')
    }
}

and the html is:

<input type="checkbox" onclick="changeStyle(this);" value="div1" />&nbsp;Div1<br />
<input type="checkbox" onclick="changeStyle(this);" value="div2" />&nbsp;Div2<br />
<input type="checkbox" onclick="changeStyle(this);" value="div3" />&nbsp;Div3<br />
<input type="checkbox" onclick="changeStyle(this);" value="div4" />&nbsp;Div4<br />
<input type="checkbox" onclick="changeStyle(this);" value="div5" />&nbsp;Div5<br />

<div id="div1" class="off">I'm in div 1</div><br />
<div id="div2" class="off">I'm in div 2</div><br />
<div id="div3" class="off">I'm in div 3</div><br />
<div id="div4" class="off">I'm in div 4</div><br />
<div id="div5" class="off">I'm in div 5</div><br />
<div id="div2" class="off">I'm in div 2</div><br />

But in this case when I have more than one div with the same id only the first div changes its style from .on to .off

How can I make so when I click on check box to change the css-style to all div tags with same id as the check box value?

Thank you in advance!

3 Answers 3

1

id must always be unique instead if id use class attribute that must work something like this

> <div class="div1 off">I'm in div 1</div><br />
Sign up to request clarification or add additional context in comments.

1 Comment

Is this correct use of getelement by class: function changeStyle(o) { if(o.checked) { document.getElementByClassName(o.getAttribute("value")).setAttribute('class','on'); } else { document.getElementByClassName(o.getAttribute("value")).setAttribute('class','off') } }
1

Elements in the DOM shouldn't have the same id; they should always be unique. Consider giving the divs the same class, eg class="div1", etc. Then do getElementsByClassName on the checkbox value.

5 Comments

Agreed, but note .setAttribute('class','off') would overwrite these classes, so that will need to change. Also, I think getElementByClassName doesn't work in most IE versions.
Good catch on the IE thing. I mostly stick with jQuery so I dont need to worry about browser compatability so I didnt realize that off hand.
So do you have any link with jquery decision of this problem?
`function changeStyle(o){ var class = $(o).val(); if(class == "on") $("."+class).addClass("on").removeClass("off"); else $("."+class).addClass("off").removeClass("on"); }'
Can you write this on jsfiddle.net please?
0

To change the css-style to all div tags. you must use classes not id's.
So change div id="div1" to div class="div1".
When you use an id, the browser will search for the ID, once it finds the FIRST id, it uses that AND STOPS searching, it doesnt not continue to look for more id's.
If you use classes, it will search the entire page for as many classes as it can find, then do whatever you want to EACH class..
So basically, change your id's to classes and everything should be fine.

Update
Here is a working JSFiddle:
Basically, first I changed the function name to lowercase (i dont know why, but "changeStyle" was not found in JSFiddle, but "changestyle" was.

BTW - Your ID's are fine, you dont need to replace them for classes. You function was just not being found.

9 Comments

But when I use class for div so I cannot toggle their style when click on check box?
function changeStyle(o) { if(o.checked) { document.getElementByClassName(o.getAttribute("value")).setAttribute('class','on'); } else { document.getElementByClassName(o.getAttribute("value")).setAttribute('class','off') } } And then <div class="div1">I'm in div 1</div><br /> <div class="div1">I'm in div 2</div><br /> Still does not work
I just updated my answer with a JSfiddle. Check it out, its working as wanted.
I am checking the preview but it still does not toggle the last div witch is with same id as div 2?
Divid2, you will have to change to a class. You can only use a ID once, If you want to use it again, you HAVE to change it to a class, then use it as many times as you want, best bet is to change them all to a class so you can use the same function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.