Why isn’t this working?
—HTML——–
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="j.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$.createHello();
});
</script>
</body>
</html>
—JS———j.js——-
(function($){
$.fn.createHello= function() {
$('body').append('<div id="hello">Hello world</div>');
$('#hello').click(funciton(){
alert("Hello you clicker");
});
};
})(jQuery);
Any solutions, or better ways to do the same thing?
Change the
clicktodelegatesince you are creating the hello element on the fly. Below is the updated code. Also function was misspelled,Live Demo
Personally this is how I would write it:
Updated Demo
This allows you to tie it to any element on the page and call
createHello()I also suggest checking out the plugin authoring reference for some good tips.