Reputation: 34623
Looking for something like:
$("input:radio:checked").previous("name", "original_name").attr("name","new_name");
I tried a few different seen around here but mostly get the error: Object Expected
Any help is appreciated.
Upvotes: 3
Views: 17958
Reputation: 787
Here's a jQuery script that does the job requested. I'm using it to rename all the input=text fields on a form. Though I'm not doing anything with radio or checkbox, I added the relevant case statements. Insert your radio/checkbox transforms as needed.
function renameTextInputFields(ele) {
var i = 0;
$(ele).find(':input').each(function() {
switch(this.type) {
case 'text':
$(this).attr("name","textFieldNumber"+(++i));
break;
case 'checkbox':
case 'radio':
});
}
Call this method on a DOM element. Myself, I'm making a clone of a div within a form. The div has lots of text fields. I get the base element like so.
var clone = $('.classAssignedToMyDiv').clone();
It runs fine in both IE 8.x and Firefox 3.x
Upvotes: 3
Reputation: 15844
I agree with the others that there is a better solution than to rename the input.
Having said that, and to answer your original question - I suggest that you assign ids to your radio inputs, and then use this code:
$("#inputId").attr("name","new_name");
I don't understand your use of .previous() - I can't find it in the jQuery documentation.
Upvotes: 2
Reputation: 32243
As Ben S wrote in the comment to your original question, why do you need to change the name?
Let's say you have
<input type="radio" name="myRadio" id="radioChoice1" value="radioChoice1Value" />
<input type="radio" name="myRadio" id="radioChoice2" value="radioChoice2Value" />
If user selects 'radioChoice1' and clicks submit, browser will send
myRadio=radioChoice1Value to your CGI script. You can process this as you desire.
In case you insist on changing the name attribute, I am not sure what is the 'previous' method in your code.
Can you try,
$("input:radio:checked").attr("name","new_name");
And see if it works?
I can imagine that there could be cross browser problems if you try to change the name attribute. You can create a hidden input and store the name of the radio button selected by the user.
<input type="hidden" id="selectedRadioButton" />
$('#selectedRadioButton').val($("input:radio:checked").attr('name'));
EDIT:
Please post your HTML so that I can tell you what kind of values will be posted to CGI.
For example if you have
<input type="radio" name="transactionType" id="buy" value="buy" />
<input type="radio" name="transactionType" id="sell" value="sell" />
If user clicks on the first radio button 'buy', then in your CGI script value for 'transactionType' will be 'buy'.
Upvotes: 7