satya satya - 4 months ago 8
Javascript Question

Can not select the radio button with value using jquery

I need one help .I need to check radio button and set the value using Jquery/Javascript.I did something but its not working.

<input type="radio" name="con" id="con" value="" onClick="checkCon();">Country
<button type="button" id="btn">Check</button>


document.getElementById('btn').onclick=function(){
var condata='123';
$("input[name=con][value=" + condata+ "]").prop('checked', true).trigger('click');
}


Here when user will click on check button it can not check the check box and can not set the value.Please help me.

Answer

try this, it can help:

 <!DOCTYPE html>
 <html>
   <head>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">          </script>
 <script>
   $(document).ready(function(){
    var condata = '123';
    $("#btn").click(function(){
       $('#con').val(condata).prop('checked', true).trigger("click");
    });
 });
 function checkCon()
  {
     alert($('#con').val());
  }
  </script>
 </head>
 <body>

    <input type="radio" name="con" id="con" value="" onClick="checkCon();">Country
    <button type="button" id="btn">Check</button>


   </body>
 </html>

this code is working I've checked this, you can also try

its probably because you are mixing up javascript and jQuery , either you should use jQuery or JavaScript. Hope the above code help. and make sure you have added jQuery library before using this code