How can I set the Name attribute of an input field using Jquery?
If "other_amount" is the ID of the other input, use attr and val as follows:
$("#resetPrompt").click(function(){
var otherValue = $("#other_amount").val();
$("#input1").attr("name", "someName");
}
This is for all the googlers out there:
It should be noted that Alex's proposal, though functional in most "real" browsers, does not work in ie 8 and below (jquery 1.6.2). When setting the "name" attribute of an input field, Microsoft, to address a bug, added a fictitious "submitName" attribute when dynamic input fields are attached to the DOM.
To workaround the problem in ie, either force your users to upgrade, use Firefox, Chrome, etc. or you'll need to add the input field manually:
$("#resetPrompt").click(function(){
$("#input1").remove();
$("#input_container").append('<input id="input1" name="' + other_amount + "' type="text" class="checked other_amount" onFocus="Toggle(\'radio1\', \'input1\');" />');
});
$('#resetPrompt').click(function(){
$('#input1').attr('name', 'other_amount');
});
Assuming that "other_amount" is the name, not id of the corresponding input.
$('#resetPrompt').click( function() {
$('#input1').attr('name', $('[name=other_amount]').val());
});