Jquery If radio button is checked Jquery If radio button is checked jquery jquery

Jquery If radio button is checked


$('input:radio[name="postage"]').change(    function(){        if ($(this).is(':checked') && $(this).val() == 'Yes') {            // append goes here        }    });

Or, the above - again - using a little less superfluous jQuery:

$('input:radio[name="postage"]').change(    function(){        if (this.checked && this.value == 'Yes') {            // note that, as per comments, the 'changed'            // <input> will *always* be checked, as the change            // event only fires on checking an <input>, not            // on un-checking it.            // append goes here        }    });

Revised (improved-some) jQuery:

// defines a div element with the text "You're appendin'!"// assigns that div to the variable 'appended'var appended = $('<div />').text("You're appendin'!");// assigns the 'id' of "appended" to the 'appended' elementappended.id = 'appended';// 1. selects '<input type="radio" />' elements with the 'name' attribute of 'postage'// 2. assigns the onChange/onchange event handler$('input:radio[name="postage"]').change(    function(){        // checks that the clicked radio button is the one of value 'Yes'        // the value of the element is the one that's checked (as noted by @shef in comments)        if ($(this).val() == 'Yes') {            // appends the 'appended' element to the 'body' tag            $(appended).appendTo('body');        }        else {            // if it's the 'No' button removes the 'appended' element.            $(appended).remove();        }    });