jquery call function on click -


i facing problem calling function on click. following code:

javascript:

$(document).ready(function(){ function showmessage(msg) { alert(msg); }; }) 

html:

<input type="button" value="ahaha" onclick="$(this).showmessage('msg');" /> 

for reasons cant use click() function. please me.

a few things:

  • showmessage() isn't property of jquery object. it's regular function.
  • if you're using jquery, don't use onclick. bind events javascript.

there few ways of fixing code. here's one:

javascript:

$.fn.showmessage = function(message) { alert(message); }; $(document).ready(function() { $('#button').on('click', function() { $(this).showmessage('i message'); }); }); 

html:

<input type="button" value="ahaha" id="button" /> 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -