Search code examples
javascripthtmljquerytextfieldhidden

How to set the value of an input hidden field to a value entered in another input text field using JavaScript?


I have an input textfield with the name qtyText, to which a user enters a value. I would like to set this value as the value for another hidden field named qtyTextHidden using JavaScript. How can I go about it?

HTML:

<input name = "qtyText" type = "textbox" size = "2" value = "" />
<input type="hidden"  value = "" name="qtyTextHidden"/>

My efforts to set the field value using JS works, but I am unable to send the value to the servlet. So, I am attempting to directly set the value using a function, and then trying to send it to the servlet. I would like to have a value = someJSFunction(), some kind. The function needs to trigger upon onChange event in the qtyText input field.


Solution

  • Using jQuery:

    $(document).ready(function() {
        $('input:text[name="qtyText"]').keyup(function() {
            $('input:hidden[name="qtyTextHidden"]').val( $(this).val() );
        });
    });
    

    Using JavaScript:

    window.onload = function() {
        if(document.readyState == 'complete') {
            document.getElementsByTagName('input')[0].onkeyup = function() {
                document.getElementsByTagName('input')[1].value = this.value;
            };
        }
    }: