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
  • The reason why you don't find how-tos explaining how to do it is because inline events are not the best way to do event handling, and jQuery has made the cross browser compatibility reason obsolete. Commented Aug 27, 2010 at 10:31

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.