tags:

views:

10

answers:

1

Following code works fine in Firefox. In IE(8.0) button "2" does not work.

$('<button type="button" onclick="alert(1)">1</button>').appendTo($('body'));
$('<button type="button" >2</button>').attr('onclick','alert(2)').appendTo($('body'));

Question: what am I doing wrong?

+2  A: 

You should attach click handlers with .click() instead, like this:

$('<button type="button">2</button>').click(function() { alert(2); }).appendTo($('body'));

This is a shortcut for .bind('click', func), though which you can bind any event, several such shortcuts are available.

Nick Craver