jquery - If input is checked not working -


i want make jquery shoot action triggered click or pressed key while input#show1 checked. way tried below seems simple way check if input checked doesn't work somehow.
did wrong in code appreciated!

<script> if ($('input#show1').is(':checked')) {     alert("show1 checked"); } </script>  <input type="radio" id="show1" name="group">  <label for="show1">show</label> <input type="radio" id="hide1" name="group" checked> <label for="hide1">hide</label> 

two options here, either execute conditional check on dom load or check on change. have to wait on document loaded, can using $(document).ready() call. calling function on click mplungjan suggested work well, though don't it. want happen when value changes unchecked checked, , not when it's checked , it's clicked again. therefore can use change.

$(document).ready(function () {     function checker() {         if ($('input#show1').is(':checked')) {             alert("show1 checked");         }     }     checker();     $('input#show1').change(checker); }); 

this code check value on page load, , watch changes user makes. in this fiddle, click "show". in one, alert show on page load because added check attribute show.


following edit.

$(document).ready(function () {     $(document).keyup(function(e) {         if (e.keycode == 27 && $('input#show1').is(':checked')) {            alert('esc key pressed.');         }     }); }); 

see in action here.


another edit.

$(document).ready(function () {     $(document).keyup(function(e) {         if (e.keycode == 27) {            checker();         }     });      $("#control").click(function() {         checker();             });      function checker() {         if ($('input#show1').is(':checked')) {             alert("it's checked.");         }         else {             console.log("bummer, mate.");         }     } }); 

seet in action here.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -