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?
+4
A:
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())
Cide
2009-07-29 19:40:38
Shouldent you put it under some kind of event, like "submit" or "keyup"?
Itay Moav
2009-07-29 19:42:45
$('input_2').keyup(function(){ $('#input_1').val($('#input_2').val());});
Stefan Kendall
2009-07-29 19:51:06
I tried this code with the keyup event, and I still can't get any data submitted in the hidden field. I can't see what I'm doing wrong.
John Stephens
2009-07-29 22:25:29
Try to figure out if it's the event or if it's the function code. You could also put this in the form's onsubmit event to reduce overhead. I also believe this event is more reliable as I've had some troubles catching keyup events myself, but I haven't fully investigated the cause.
Cide
2009-07-29 22:32:06
I'm not very familiar with Javascript. How can I identify whether the problem is in the event or function code? Here is an example with two input fields that I want to keep in sync: http://designop.us/jtk-temp/21.htmPlease let me know if you notice any errors in my code, or if you can tell me how to pass input from the first input element into the other. Thanks!
John Stephens
2009-07-29 23:46:42
A:
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
RaYell
2009-07-29 19:41:11
+1
A:
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.
Peter Di Cecco
2009-07-29 19:55:38
The form id is generated uniquely each time by a script. I've tried the text-change trigger method, and I can't get any data submitted in the #hidden_id field. Is this dependent on a certain version of jquery? My script tag links to http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
John Stephens
2009-07-29 22:14:26
The text change event is a little tricky. The text isn't usually considered changed until the text box loses focus. I'm not sure if the event is triggered when the text box is changed and then the form is submitted without the text box first losing focus. Why can't you copy the value after the form has been submitted in the script that receives the GET or POST request?
Peter Di Cecco
2009-07-30 00:11:25