views:

435

answers:

2

Hello, I have an onclick event attached to a button. Clicking the button adds a text box to the target div.

the code is: onclick="return addBlank("param1","param2"); The function addBlank does not have a return statement.

Clicking on the button behaves as intended in firefox, opera,chrome and safari - i.e., an empty text box is added to the target div and page is not reloaded.

Clicking on the button in ie8 forces a page reload and doesnt add anything to the target div.

Is there an onclick incompatibility that I'm missing here?

Thanks, I appreciate your taking the time to read this doubt!

+1  A: 

Why not add

return false;

to the end of the function then? It should prevent anything happening as a result of the onclick event.

Geoff
+1  A: 

IE is treating the button as a submit actor (i.e., clicking it submits the form)

The click handler needs to return false; to prevent this.

function addBlank( p1, p2 )
{
  // stuff
  return false;
}
Peter Bailey
or preventDefault()
meo
That's not an IE method - IE would use `event.returnValue = false;`. In this case it's easier to just return false from the handler to prevent default behavior.
Peter Bailey
Hi,I did try this - and it worked in 1 case only. In the second case - with a similar function - the page still reloads.I have tried onclick="return x();", onclick=x(); and x() has a return false statement, and both : onclick=return x(); with x() having a return false statement. None are working. Any thoughts?
Mallika Iyer
I'm confused - you are saying this didn't work, but marked Geoff's answer (which is the same answer) as accepted? So did you solve your problem or not?
Peter Bailey