rrapuya
rrapuya

Reputation: 298

Jquery get value of radio button

here's my html mark up

<span class="wpcf7-form-control-wrap radio-20">
    <span class="wpcf7-radio radio-20" id="radio-20">
      <span class="wpcf7-list-item">
        <span class="wpcf7-list-item-label">Yes</span>
        <input type="radio" value="Yes" name="radio-20">
      </span>
      <span class="wpcf7-list-item">
        <span class="wpcf7-list-item-label">No</span>
        <input type="radio" value="No" name="radio-20">
      </span>
    </span>
</span>

how would i know of the "Yes" radio button or the "No" is checked?

i tried:

    jQuery("span#radio-20 span input").val("Yes").click(function (){
            alert('bang!');
    });

but it will alert also when i click "No";

can somebody Help me?

Upvotes: 1

Views: 1106

Answers (3)

silviubogan
silviubogan

Reputation: 3461

You can do this:

var val = $("#radio-20").find(":checked").val();
if (val === "Yes") {
    // "Yes" button checked
} else {
    // "No" button checked
}

If you want a function to be called when a button is checked, you can do this:

$("#radio-20 input").click(function () {
    var val = $(this).val();
    if (val === "Yes") {
        // "Yes" button checked
    } else {
        // "No" button checked
    }
});

(I have not tested these but it should work)

Upvotes: 4

Tomalak
Tomalak

Reputation: 338148

jQuery("span#radio-20 span input[value=Yes]").click(function (){
  alert('bang!');
});

Upvotes: 3

genesis
genesis

Reputation: 50966

 jQuery("span#radio-20 span input").click(function (){
            if ($(this).val() == "Yes"){
                alert('bang!');
             }
    });

Upvotes: 3

Related Questions