Question

HTML:

From: <input id="from" name="from" type="text" class="form-control input-md">
To: <input id="to" name="to" type="text" class="form-control input-md">

<input type="text" name="fromHidden" value="">
<input type="text" name="toHidden" value="">

jQuery:

<script type="text/javascript"> 

    $(document).ready(function() {

        $('#from').click(function() {
            var input = $( '#from input').val();
            $("#fromHidden").attr("value", input);
        });

        $('#to').click(function() {
            var input = $( "#to").val();
            $("#fromTo").val(input);
        });

    }); 

</script>

I want to get the values from the HTML input fields, 'to' and 'from', and add them to the hidden text fields 'toHidden' and 'fromHidden'. This is to be done at any time the 'to' and 'from' are changed.

The jQuery I have wrote isn't currently working.

Any help would be appreciated.

Was it helpful?

Solution

You can use attribute selector for name to get the named tags.

$('#from,#to').change(function () {
    $("[name=fromHidden]").val($('#from').val());
    $("[name=toHidden]").val($('#to').val());

});

OTHER TIPS

  $(document).ready(function() {

        $('#from').blur(function() {
            var input = $( '#from').val();
            $("input[name=fromHidden]").val(input);
        });

        $('#to').blur(function() {
            var input = $( "#to").val();
            $("input[name=toHidden]").val(input);
        });

    }); 

If you want to update the values on change then use following script

$('.form-control').on('input propertychanged', function () {
    var input = $(this).val();
    $('[name=' + this.id + 'Hidden]').val(input);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top