Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change button name

Status
Not open for further replies.

jasonlfunk

Programmer
Dec 28, 2006
2
US
I want to change the name of a button in a function. The reason: I am going to have many buttons on a page and when the user clicks it- it changes into a submit button. I want to be able to change the name of the button (the actual name, not the value) to something that the action handler will be able to look for.

my first guess of document.form.button.name = "newName"; didn't work.

Is this possible?
 
I will have something like this:

<input type=button name="modify1" onClick="change(1)">
<input type=button name="modify2" onClick="change(2)">
<input type=button name="modify3" onClick="change(3)">
<input type=button name="modify4" onClick="change(4)">


function change(id)
{
name = "modify" + id;
document.form[name].value = "Save";
document.form[name].attributes["onclick"].value = "submit()";
document.form[name].name = "modify_submit";
}

The action of the form will be a php page that contains


if(isset($_POST['modify_submit']))
{
...
}


 
Ok I see what you are wanting. The cleanest way I see to go about this is to assign IDs instead of names for the buttons, the code would look like the following for the HTML:
Code:
<input type="button" id="modify1" value="ButtonText" onClick="change(this)">
<input type="button" id="modify2" value="ButtonText" onClick="change(this)">
<input type="button" id="modify3" value="ButtonText" onClick="change(this)">
<input type="button" id="modify4" value="ButtonText" onClick="change(this)">

I passed the button object to the function, from there I can do with the object what I want.

Code:
function change(buttonObj) {
   buttonObj.value = "Save";
   buttonObj.onclick = submit();
   buttonObj.id = "modify_submit";
}

Not sure why you would need to change the ID or Name, and I don't recommend naming a function submit() cause that is the method used to submit forms. But this should work barring a syntax error I may have made.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top