Input value hidden using HTML and JavaScript

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!

Last updated:8/17/2013 6:30:36 AM

1 Answers

shreesh chandra shukla
shreesh chandra shukla

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