views:

17

answers:

1

I want to wrap the following code into a function using jQuery and call that function from inline (eg: onclick, onchange etc.).

function some_function() {
   alert("Hello world");
}

Called by (example):

<input type="button" id="message" onclick="some_function()" />

This question is simple for a reason. I can't seem to find a proper jQuery how-to.

  • Should I wrap that function into a jQuery $(document).ready() ?
  • Should make a normal javascript function and use $(document).ready() in that function?
+1  A: 

You should not use that inline event handler to go with jQuery.

Use unobtrusive code:

function some_function() {
  alert("Hello world");
}

$(document).ready(function(){
   $('#message').click(some_function);
});
jAndy