Search code examples
javascriptdom-events

Firefox onchange not calling function


I have a form that contains input:

<input onchange="checkDomain(true); return false;" type="text" id="dsearch" value="" name="domain" maxlength="30"/>

Which works just fine in Opera, Chrome and IE - but Firefox and Safari are having problems with calling the function checkDomain(). I added a line into the function for debugging:

function checkDomain(check) 
{
console.log('checkDomain() called!'); 
// do rest of the stuff...
}

So, Chrome/Opera/IE calls the function with no problem after you enter text and click somewhere else - but Firefox/Safari doesn't. Anyone have a clue?


Solution

  • I cannot remember where I read it, but you should use .keydown()/.keyup()/.keypress() (whatever suits your needs) instead of onchange. At least for input[type=text].

    you can use .blur() and .focus for their respective purposes too.

    Seeing this in your question: "after you enter text and click somewhere else", makes me conclude you need the .blur() function.

       $("input[type=text]").blur(function(){
           console.log('checkDomain() called!')
       });