change div text on radion button click

Posted by pisi on Stack Overflow See other posts from Stack Overflow or by pisi
Published on 2012-10-23T10:44:38Z Indexed on 2012/10/23 11:00 UTC
Read the original article Hit count: 148

Filed under:
|

I want to put the radio button value in the status div. The text of status div should change, according to the radio button selected by the user. The code that I used bellow is not working. Please help. Thanks!

HTML code:

 <form action="">
     <input type="radio" name="sex" value="male">Male<br>
     <input type="radio" name="sex" value="female">Female<br>
     <div id="status"></div>
  </form>?

JS code:

 $(document).ready(function () {
    RadioStatus="";

    $("input[type='radio']:checked").each( function()
    { 
        if ($(this).attr('checked');)
              RadioStatus=$(this).val();
              $("#status").text(RadioStatus);
        });

    $("input[type='radio']").change(function()  
     {
             RadioStatus= $('input[type='radio']:checked').val()
            $('#status').text(RadioStatus);        
    });
});

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery