UserRA
UserRA

Reputation: 185

set the value of the input field as value of the hidden field using JS

This question seem to be very silly! but I am looking for the best answer,

I have a input field and a hidden field. How to set the value of the input field as value of the hidden field?

<input class="class1" id="id1" name="name1" value="06/30/2015" />

<input type="hidden" name="hidden" id="hidden" value="I want the value of above field">

Upvotes: 0

Views: 647

Answers (3)

Rino Raj
Rino Raj

Reputation: 6264

Please try this

$(document).ready(function(){
    $("#id1").change(function(){
        $("#id1").val($("#hidden").val());
    });
});

DEMO

Upvotes: 1

Bharat Ranpariya
Bharat Ranpariya

Reputation: 1253

You can this by below why, i used onblur for set values from input to hidden. same why you can use different method for this.

Or if you strictly want to use javascript then add one onblur function and do code as i do with callthis() function.

$("#id1").blur(function(){
      $("#hidden").val($(this).val())
      console.log($("#hidden").val());
    });

function callthis(inputobj)
{
  document.getElementById("hidden").value = inputobj.value;
  console.log(document.getElementById("hidden").value)
}
<input class="class1" id="id1" name="name1" onblur="callthis(this)" value="06/30/2015" />

<input type="hidden" name="hidden" id="hidden" value="I want the value of above field">

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You can use a change event handler to update the value of hidden field

//set the initial value
hidden.value = id1.value;
<input class="class1" id="id1" name="name1" value="06/30/2015" onchange="window.hidden.value=this.value" />
<input type=hidden name="hidden" id="hidden" value="I want the value of above field">

Upvotes: 1

Related Questions