Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  70] [ 6]  / answers: 1 / hits: 22041  / 12 Years ago, wed, january 9, 2013, 12:00:00

This one is a real headache. I have three checkboxes in a form that I have labels for, and the labels have background images set with CSS, the question is, how exactly do I get my JavaScript to change the image from current image to a second image when it is clicked and select the checkbox. From what I know of javascript, it should work, any ideas? Also, it does have to be this way due to previous issues with i.e. 7 and 8.



HTML



<input type=checkbox name=interest1 id=interest1 value=interestedstyle=display:block>
<input type=checkbox name=interest2 id=interest2 value=interestedstyle=display:block>
<input type=checkbox name=interest3 id=interest3 value=interestedstyle=display:block>
<p align=center>
<label for=interest_inet id=label-interest1 onClick=func()></label>


CSS:



label {
border:1px solid red;
display:inline-block;
}

#label-interest1 {
background-image: url(images/internetbutton.gif);
width: 152px;
height:152px;
}


JavaScript:



<script type=text/javascript>
function func()
{
if(document.getElementById('interest_inet').checked)
{
document.getElementById('label-interest1').style.backgroundImage=url(images/internetbutton.gif);
}
else
{
document.getElementById('label-interest1').style.backgroundImage=url(internetbuttonchecked.gif);
}
}
</script>


Here is a live code example


More From » html

 Answers
14

You seem to just be missing some quotes (the value of style.backgroundImage should be a string):



function func()
{
var label = document.getElementById('label-interest1');
if(document.getElementById('interest_inet').checked)
{
label.style.backgroundImage = 'url(images/internetbutton.gif)';
}
else
{
label.style.backgroundImage = 'url(internetbuttonchecked.gif)';
}
}

[#80996] Monday, January 7, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leiaf

Total Points: 10
Total Questions: 101
Total Answers: 84

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
leiaf questions
Sat, Mar 27, 21, 00:00, 3 Years ago
Wed, Apr 3, 19, 00:00, 5 Years ago
Wed, Jan 16, 19, 00:00, 6 Years ago
;