INPUT VALUE HIDDEN USING HTML AND JAVASCRIPT

Jeet Verma

Total Post:110

Points:776
Posted by  Jeet Verma
 1759  View(s)
Ratings:
Rate this:
Hi developers!

I have this input:

<input id="tag1" type="text" name="tags" class="tags" value="@Model.list" />

and I want to get this input value in a hidden input, so I used this:

<input type="hidden" name="tag" value="tags" />

Instead of getting the true value of the first input, I only get the string "tags"! Can you please tell me how to obtain the true value of the first input in my hidden input?

 Thanks!

  1. shreesh chandra shukla

    Post:105

    Points:735
    Re: Input value hidden using HTML and JavaScript

    Hi Jeet!

    I don't understand why you would want to copy the value of one input field to another (albeit, hidden). But if that is what you want to do, try using the below code.

    The function attached to the onblur event of the input field would set the value of the input field to the hidden field whenever it loses focus.

    The window.onload=setValue; will do the same on page load.

    HTML

    <input id="tag1" type="text" name="tags" class="tags" value="@Model.list" onblur="setValue();" />

    <input type="hidden" name="tag" value="tags" id="tag1_hidden" />

    JS

    function setValue() {

        document.getElementById("tag1_hidden").value = document.getElementById("tag1").value;

    }

    window.onload = setValue;

    Best of luck!

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!