Skip to main content

JQuery Custom Image Checkbox code not toggling



I am using the following code to make a custom checkbox with my own images.





First I included JQuery and added the JQuery code for the required functionality:







<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<script>

$(document).ready(function(){



$("#moreinfo").change(function() {

if(this.checked) {

$(this).prev().attr("src", "checkbox_unchecked.gif");

} else {

$(this).prev().attr("src", "checkbox_checked.gif");

}

});



});



</script>





Next...here's the HTML:







<label for="moreinfo">

<img src="checkbox_unchecked.gif"/>

<input name="moreinfo" type="checkbox" id="moreinfo" style="display:none">

</label>







The unchecked image is there but when I click on it, it doesn't change/toggle.





I'm I missing something?





UPDATE:





Testing this full code locally and it's not working...







<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<script>

$(document).ready(function(){



$("#moreinfo").change(function() {

if(this.checked) {

$(this).prev().attr("src", "http://icdn.pro/images/en/v/e/verify-correct-icone-9268-48.png");

} else {

$(this).prev().attr("src", "http://www.theology.edu/Remata/Android/Help/wrongx_icon.png");

}

});





});



</script>



</title>









</head>

<body>







<label for="moreinfo">

<img src="http://www.theology.edu/Remata/Android/Help/wrongx_icon.png"/>

<input name="moreinfo" type="checkbox" id="moreinfo" style="display:none">

</label>









</body>

</html>




Comments

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...