1

Internet Explorer 7 does not handle buttons correctly since it sends the text between <button> and </button>rather than the value. (Weird but true.) This Javascript is supposed to correct this by sending the value of the button onClick. But it does not work. Can anybody see the error ?

I use this to attach the Javascript file in HTML5 :

<script src="buttonfix.js"></script>

Contents of buttonfix.js :

function runonclick()
{
    var count = 0;
    var formlength = this.form.elements.length;
    while(count < formlength)
    {
        if(this.form.elements[count].tagName === "button")
        {
            this.value = this.attributes.getNamedItem("value").nodeValue;
        }

        count++;
    }
}

function buttonfix()
{
    var buttonarray = document.getElementsByTagName("button");

    var count = 0;
    var buttonarraylength = buttonarray.length;
    while(count < buttonarraylength)
    {
        buttonarray[count].onClick = runonclick();

        count++;
    }
}

window.attachEvent("onload", buttonfix());
2
  • 1
    What exactly does not work? Commented Jun 25, 2011 at 14:02
  • @Felix Kling The Javascript does not make the button send the value instead of the text. I used jslint so the problem must not be syntax related. Commented Jun 25, 2011 at 14:26

1 Answer 1

1

I suggest you to try:

if(this.form.elements[count].tagName.toLowerCase == "button");

instead of:

if(this.form.elements[count].tagName == "button")

because the tagName property generally returns an uppercased string.

Another line does not work:

buttonarray[count].onClick = runonclick();

As JavaScript it is a case-sensitive language, you have to use the standard name of the event (onclick).

buttonarray[count].onclick = runonclick();

However, you may still to use onClick in your HTML (but not XHTML).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.