views:

2836

answers:

2

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.

+2  A: 

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'.

SolutionYogi
$("input:radio:checked").attr("name","new_name");object error
Tommy
how would the cgi know to execute the buy or sell function with myRadio=radioChoice1Value? What differentiates that?
Tommy
That was just an example. Post your HTML so that we can discuss more. Also, I updated my answer with another example.
SolutionYogi
+2  A: 

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.

Chris B