Search code examples
jqueryinputradio-buttonoutput

JQuery get radio input value and PRINT IT


I've been browsing for this for three hours now... and I found only how to GET the value from the radio input. But how do I print it? Let's say I have this input form:

<form action="" method="">
<input type="radio" name="time" value="7">7
<input type="radio" name="time" value="14">14
<input type="radio" name="time" value="21">21
<input type="radio" name="time" value="28">28
</form>

and this script

var time = $('input[name=time]:radio:checked').val();

to get value... But how do I output it? I've created <p id="timeDisp"></p> and user this: $("#timeDisp").text(time); but no success. So how do I do it?


Solution

  • There is no pre-selected radio button in your markup and as you are using checked selector, val method on page load returns undefined, you can listen to change event.

    $(function() { // when the DOM is ready
       $('input[name=time]').change(function() { // listen to change event
          $("#timeDisp").text(this.value);
       })
    })