Inline html attribute event handlers can only call global functions. Your clear() function is not global because it is defined inside your document handler, so your onclick="clear()" cannot find it. You need to either move the function outside the finished handler (making it global), or, better, bind the click using jQuery:
$(document).ready(function(){ $(this).mousedown(function(e){ var $x = e.pageX; var $y = e.pageY; var $div = "<div class='theDiv' style='width: 250px; height: 150px; background-color:#e7e7e7; position: absolute; top:" + $y + "px; left:" + $x + "px;'>Hey</div>"; $("body").prepend($div); }); $(".clear").click(function () { $(".theDiv").remove(); }); });
Notice also that I added var before the variables in the mousedown handler: without var they become global variables, which just makes your code more error prone and hard to debug. (If you have a good reason, why should they be global?)
nnnnnn
source share