javascript - Jquery selector help -


i have following:

<form id="my_form">   <input type="checkbox" name="a" value="a"> check <img src="..." /> </form> 

to handle check , uncheck events (and works):

$('#my_form :checkbox).click(function() {    if($(this).is(':checked')) {       //...    }    //... }); 

my question is: inside of click function, how can select <img> tag right beside checkbox? can without specifying id image? ..by "select" mean need able hide() , show() it. thanks

you can use .next() since it's next (immediate next, important) sibling element, this:

$('#my_form :checkbox').click(function() {    if(this.checked) {       var img = $(this).next('img');    } }); 

i think you're after easiest done .toggle(bool), this:

$('#my_form :checkbox').change(function() {    $(this).next('img').toggle(this.checked); }); 

this show <img> when checked, , hide when it's not. note used .change() here, since that's event want when dealing checkbox correct state.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -