Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a radio button is checked jquery [duplicate]

Tags:

jquery

I have this HTML:

<input type="radio" name="test" id="test" value="1"><br>
<input type="radio" name="test" id="test" value="2"><br>
<input type="radio" name="test" id="test" value="3"><br>

<input type="button" onclick="CreateJobDo">

When the page loads, none of them will be checked. I want to use jQuery to check if one of the radio buttons is checked when the button is pressed.

I have this:

function CreateJobDo(){
    if ($("input[@name=test]:checked").val() == 'undefined') {

        // go on with script

    } else {

        // NOTHING IS CHECKED
    }
}

But this does not work. How to get it working?

like image 653
Mbrouwer88 Avatar asked Mar 20 '13 17:03

Mbrouwer88


People also ask

How do you check whether a radio button is checked or not in jQuery?

We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.


5 Answers

First of all, have only one id="test"

Secondly, try this:

if ($('[name="test"]').is(':checked'))
like image 126
AlienWebguy Avatar answered Nov 02 '22 11:11

AlienWebguy


try this

if($('input:radio:checked').length > 0){
// go on with script
 }else{
    // NOTHING IS CHECKED
 }
like image 22
Mohamed Habib Avatar answered Nov 02 '22 12:11

Mohamed Habib


Something like this:

 $("input[name=test]").is(":checked");

Using the jQuery is() function should work.

like image 24
RestingRobot Avatar answered Nov 02 '22 12:11

RestingRobot


if($("input:radio[name=test]").is(":checked")){
  //Code to append goes here
}
like image 34
AlphaMale Avatar answered Nov 02 '22 11:11

AlphaMale


  $('#submit_button').click(function() {
    if (!$("input[@name='name']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
      alert('One of the radio buttons is checked!');
    }
  });
like image 28
Ray Avatar answered Nov 02 '22 10:11

Ray