Fix for IE button bug -
01-03-2006
, 05:50 AM
I noticed a bug in IE, when using button-elements instead of input submits.
One button works okay, but when you use multiple button-elements on the same
form, they are all submitted, instead of the one that was clicked. So I
wrote a little script to workaround it, I attach an onclick-event to each
button on the page, and once the button is clicked, it loops through all
button-elements on the form disabling them, and finally enabling the one
that was clicked.
I use this as a separate .js file and include it to every page that uses
multiple button-elements.
and I use the following syntax to include it only when the browser is IE:
<!--[if IE]>
<script type="text/javascript" src="buttonfix.js"></script>
<![endif]-->
buttonfix.js:
----
function buttonfix(){
var buttons = document.getElementsByTagName('button');
for (var i=0; i<buttons.length; i++) {
buttons[i].onclick = function () {
for(j=0; j<this.form.elements.length; j++)
if( this.form.elements[j].tagName == 'BUTTON' )
this.form.elements[j].disabled = true;
this.disabled=false;
}
}
}
window.attachEvent("onload", buttonfix);
----
I hope this helps someone who is struggling with the same thing. Also: if
you find something to correct or something that can be improved, please let
me know.
(If you wonder why I wanted to use buttons instead of input-elements, the
answer is that button elements allows html-formatting and images inside the
button, so you can insert images inside the submit buttons that way or
underline certain letter from the button label. That's why.)
Sincerelly,
Kimmo Laine
--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam (AT) outolempi (DOT) net | Gedoon-S @ IRCnet | rot13(xvzzb (AT) bhgbyrzcv (DOT) arg) |