I just got bit by this bug today, took me a while to figure out what was going on. I was using <button> for precisely the reason that it lets you set a different label ("Edit Foo") from its value ("edit-<foo's id>").
My solution was this bit of javascript:
function submitAndRedirect(submitButtonID, redirectCode) { var submitButton = document.getElementById(submitButtonID); var redirectElementID = "input_submit_redirect_" + submitButton.form.id; if (document.getElementById(redirectElementID)) { document.getElementById(redirectElementID).value = redirectCode; } else { element = document.createElement("input"); element.name = "submit_redirect"; element.id = redirectElementID; element.type = "hidden"; element.value = redirectCode;
I didn't want the user to see the name of the button suddenly change to a piece of code the moment it's clicked on. My solution involves a longer bit of javascript, but it's a one-time cost since every page that uses just links to a site-wide .js file, and the in-button portion is pretty simple. Plus it's completely transparent to the user.
I've made a slight change to it, in that the button HTML code is now:
Comment
I just got bit by this bug today, took me a while to figure out what was going on. I was using <button> for precisely the reason that it lets you set a different label ("Edit Foo") from its value ("edit-<foo's id>").
My solution was this bit of javascript:
function submitAndRedirect(submitButtonID, redirectCode) {
var submitButton = document.getElementById(submitButtonID);
var redirectElementID = "input_submit_redirect_" + submitButton.form.id;
if (document.getElementById(redirectElementID)) {
document.getElementById(redirectElementID).value = redirectCode;
} else {
element = document.createElement("input");
element.name = "submit_redirect";
element.id = redirectElementID;
element.type = "hidden";
element.value = redirectCode;
submitButton.form.appendChild(element);
}
}
Then changed the <button> code to:
<input type="submit" id="button_99_edit" name="submit" value="Edit Foo" onclick="submitAndRedirect('button_99_edit', 'edit-99')"/>
It creates a hidden form item that my server-side code can look for, and gives it the value 'edit-99'.
I *would* add this to my list of items I'm praying Microsoft fixes in IE 7, but I don't think there's room left. It's already so big.
Replies
Shame it doesn't work without javascript though.
You could make it much simpler if you'd just write you input tag like this:
<input type="submit"
name="99"
value="Edit this"
onclick="this.value=this.name;this.name='idvalue'"
/>
...or I just don't understand your particular application problem.
I didn't want the user to see the name of the button suddenly change to a piece of code the moment it's clicked on. My solution involves a longer bit of javascript, but it's a one-time cost since every page that uses just links to a site-wide .js file, and the in-button portion is pretty simple. Plus it's completely transparent to the user.
I've made a slight change to it, in that the button HTML code is now:
<input type="submit" id="button_99_edit" name="submit" value="Edit Foo" onclick="submitAndRedirect(this.id, 'edit-99')"/>
(Replaced hard-coded ID with this.id)
Just pass 'this' and then you don't have to do an ID lookup.
Thank you for this solution. This has had me stumped for a few days. I think I still have a few hairs left that didn't get pulled out.