jQuery input value

The .val() method is used to get or set the value of form elements such as input, checkbox, radio button, select and textarea.

js:


$(function(){

    $('.old-value').text( $('input').val() ); // get the input value

    $('input').val('changed input value'); // set the input value

    $('.click').click(function() {

        $('.old-value').text( $('input').val() );

    });

});​

html:


<div class="well">

    <span class="click">get value</span>

    <input type="text" value="input value">

    <span class="old-value"></span>

</div>

Other examples with jquery .val():


$('select.foo option:selected').val(); // get the value from a dropdown select

$('select.foo').val(); // get the value from a dropdown select even easier

$('input:checkbox:checked').val(); // get the value from a checked checkbox

$('input:radio[name=bar]:checked').val(); // get the value from a set of radio buttons

Leave a Comment