Question

How can I pass input from a text-type input element into a hidden input element in the same form on a site that has jquery installed?

Was it helpful?

Solution

Triggered when the text changes:

$("#text_id").change(function(){
  $("#hidden_id").val($("#text_id").val())
});

Triggered when the form submits:

$("#form_id").submit(function(){
  $("#hidden_id").val($("#text_id").val())
});

Check out other jQuery Events.

OTHER TIPS

If input_1 is the id of the input you want to populate, and input_2 the id you want to populate from, use:

$("#input_1").val($("#input_2").val())
$("#id_of_hidden_input").val($("#id_of_text_input").val());

Assuming that you have only one field of each type:

$('input[type=hidden]').val($('input[type=text]').val())

If you have more add IDs to selectors

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top