views:

387

answers:

6

I want to know if its possible to change the name of the input tag with javascript or jquery, for example in this code :

<input type="radio" name="some_name" value="">

I want to change the some_name value when user select this radio button.

the reason what i want to do this is described here : http://stackoverflow.com/questions/1324703/calculation-radio-buttons-values-in-jquery

+6  A: 

Simply elem.name = "some other name" or elem.setAttribute("name", "some other name") where elem is the element you want to alter.

And to do that on selection, use the onchange event:

<input type="radio" name="some_name" value="" onchange="if(this.selected) this.name='some other name'">

And to apply that behavior to every radio button with that name:

var inputElems = document.getElementsByTagName("input");
for (var i=inputElems.length-1; i>=0; --i) {
    var elem = inputElems[i];
    if ((elem.type || "").toLowerCase() == "radio" && elem.name == "some_name") {
        elem.onchange = function() {
            if (this.selected) {
                this.name = "some other name";
            }
        };
    }
}

But using jQuery for that is quite easier.

Gumbo
Just using your code.. the for loop should be`for (var i=inputElems.length-1; i>=0; --i) {`
Jason Jong
@Jason Jong: Oh yes, you’re absolutely right. Thanks!
Gumbo
+4  A: 

The jQuery way

$('input:radio[name="some_name"]').attr('name', 'new name');

Gumbo has the vanilla JavaScript way covered

Russ Cam
+2  A: 

Yes, you can change the name of any element with javascript. Keep in mind though that IE 6 and 7 have trouble with submitted forms where the input elements have been tinkered with in javascript (not sure if this exact case would be affected).

$('input:radio[name="some_name"]').attr('name', 'new_name');

Edit: To change it only when it is selected, here is the code for that:

$("input:radio[name='some_name']").click(function() {
  if ($(this).attr('checked'))    $("input:radio[name='some_name']").attr('name', 'new_name');
  else                            $("input:radio[name='some_name']").attr('name', 'some_name');
});
tj111
Regarding your edit: it's probably a bad idea to change the name of a single radio button, unless there are no other radio buttons sharing that name (in which case, he should really be using a checkbox).
Shog9
@shog9: Thanks for pointing that out, didn't think about it when I posted it. I updated it so it would change all the inputs with a name of `some_name` instead of just the one that was clicked.
tj111
@tj111: excellent! But you might want to double-check that last selector...
Shog9
@shog9: Thanks again, jQuery isn't my main framework so I get some syntaxes mixed up all the time.
tj111
Heh, the quotes are a good idea, but what i meant was, the last selector will select buttons with the name you intend to give them (select buttons with name=`some_name`, change name to `some_name`)!
Shog9
A: 

Sure. If jQuery is your poison, this should do the trick:

$("input[name=some_name]").attr("name", "other_name");
Eugene Lazutkin
Doesn't really add anything to Russ's or tj111's answers.
Shog9
Sorry for that. I answered before others and couldn't know what they were going to answer. Looking forward to more of your constructive criticism!
Eugene Lazutkin
A: 

I came up with this:

<input type="radio" name="some_name" value="" id="radios">

<script type="text/javascript" charset="utf-8">
$(document).ready(function()
{
    $("#radios").click(function()
    {
        $(this).attr("name", "other_name");
    });
});
</script>
MrHus
This will change the name of only a single radio button out of a group; this may be what he wants, but i can't imagine why. See my comments above...
Shog9
A: 

Trying to change the name attribute of a radio button will cause strange, undesirable behavior in IE.

The best way to handle this is to replace the old radio button with a new one. This post may help you. If you are using jQuery, you can do it with the replaceWith function.

More information about changing name attributes in IE.

mikez302