0

I need to check if the radio button input field is checked or not using Javascript/Jquery. Actually I am setting the radio button field dynamically but while checking whether this field is checked or not it's showing me the result false where it is really checked. My code is below.

<input type="radio" name="answer_type0" id="answer_type0" onClick="selectScale(this.value,'0');" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype'];  ?>

My javascript code is below.

$('#answer_type0[value="' + qdata[0].answer_type + '"]').prop('checked', true).trigger('click');    
console.log('check',document.getElementById('answer_type0').checked);

Here my radio input field seems checked on the UI page but while i am checking the console.log message its showing false. Here I need if the radio button field is checked it should give the message true but it's not happening like that.

2
  • Please include the rendered html, not just the php version. Commented Jul 16, 2016 at 7:40
  • Can you set up a jsfiddle.net showing the problem? Commented Jul 16, 2016 at 7:41

2 Answers 2

1

Try checking like this.

if($('#answer_type0').is(':checked')){
   console.log("true");
}else{
   console.log("false");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Actually my problem is the radio button is checked but its showing false.
Did you try using my answer to check if it is false?
I did it like this console.log('check',$('#answer_type0').is(':checked')); but its giving the result false but actually the radio button is checked.
I did as per you and got the result what i have explained above.
0

you can check it by the following methods

 if ($("#answer_type0").prop("checked")) {
          alert("checked");
        }
      else
      {
      alert ("not checked");
      }

        // OR
        if ($("#answer_type0").is(":checked")) {
           alert("checked");
        }
       else
       {
       alert ("not checked");
       }

3 Comments

Its not the solution.Here my radio button field is checked but its showing as false.
then the problem is with the $('#answer_type0[value="' + qdata[0].answer_type + '"]') check whether the check box value is same as in html that's why it returns false
So whats the solution for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.