Tofandel
Tofandel

Reputation: 3555

Javascript: Error, object is not a function

I have a strange issue with this very small code :

function autre(element)
{
    if (element.value=="autre") 
    {
        var inputText=document.createElement("input");
        inputText.setAttribute("type", "text");
        inputText.setAttribute("name", "autre");
        inputText.setAttribute("required", "");
        element.parentNode.appendChild(inputText);      
    }
    else element.parentNode.removeChild(element.nextSibling);
}

and HTML

<select name="matiere" onchange="autre(this);">
    <option value="autre">Autre</option>
    <option value="...">Some others options</option>
</select>

Wich create an input text when the 'autre' value of a select is selected.. This works fine.. But when I change the value again I get a weird error: object is not a function..

Any idea ?..

Edit : The problem was as ceakki mentionned the name of the function. Thanks! Thank you everybody for your help

Upvotes: 2

Views: 1228

Answers (2)

Cristian Ciocău
Cristian Ciocău

Reputation: 1064

Change the name of the autre() function or of the new created element <input name="autre" ... />.

It seems that IE9 appends the name autre of the <input name="autre" ... /> to the current Window object (or global object).

In this case autre will refer to the new created element and not to your function.

Upvotes: 2

Sirikon
Sirikon

Reputation: 426

That script works well for me http://jsfiddle.net/J7acJ/

Just added an eventListener

document.querySelector("select").addEventListener('change', function(){
    autre(this);
})

Upvotes: 1

Related Questions