1

I need one help.I need to check the radio button and set its value using Javascipt/Jquery.I am explaining my code below.

<input type="radio" name="answer_type0" id="answer_type0" value="" onClick="selectScale();">Male
    <button type="button" id="btn">Edit</button>
    <script >
        document.getElementById('btn').onclick=function(){
            var qdata='123';
            $('#answer_type0[value="' + qdata + '"]').prop('checked', true).trigger('click');
            //$('#answer_type0').prop("checked",true);
            console.log('checked',$("#answer_type0").prop("checked"));
        }
        function selectScale(){
            console.log('value');
        }
    </script>

On the above code when user will click on Edit button the radio button should check and the click should trigger. But in my case its not happening like this. Here the plunkr is present.Please help me.

5
  • Can you make this correct ? Commented Jul 16, 2016 at 10:56
  • 1
    In the selector you're expecting the input to have a value attribute which it doesn't. Commented Jul 16, 2016 at 10:57
  • check this demo Commented Jul 16, 2016 at 10:58
  • i have tried also what you have done.i need to set its value also while clicked. in this case its not coming. Commented Jul 16, 2016 at 10:59
  • 2
    4 hours ago there was this exact same question Commented Jul 16, 2016 at 11:00

2 Answers 2

0

Try like this.

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
  </head>
  <body>
    <input type="radio" name="answer_type0" id="answer_type0" value="" onClick="selectScale();">Male
    <button type="button" id="btn">Edit</button>
    <script >
        $(document).ready(function(){
          document.getElementById('btn').onclick=function(){

            $('#answer_type0').prop("checked",true);
        }
        function selectScale(){
            console.log('value');
        }
       });
    </script>
  </body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Here is answer

<input type="radio" name="answer_type0" id="answer_type0" value="" onClick="selectScale();">Male
<button type="button" id="btn">Edit</button>
<script>
  document.getElementById('btn').onclick = function() {
    var qdata = '0';
    $('[name=answer_type' + qdata + ']').prop('checked', true).trigger('click');
    console.log('checked', $("#answer_type0").prop("checked"));
  }

  function selectScale() {
    var qdata = '0';
    $('[name=answer_type' + qdata + ']').val('some_value');
    console.log('some_value');
  }

</script>

6 Comments

Please test inside my plunkr.
Not working? Can you explain what not working? Here is jsfiddle: jsfiddle.net/qLyx07ch
Ok,But i need to set also its value dynamically.
Tell me where you want to set value? Explain more
check this line ` $('#answer_type0[value="' + qdata + '"]').prop('checked', true).trigger('click');` from my code.
|

Your Answer

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