Custom function call in jquery

I am trying to call a custom function in jquery.

$(document).ready(function() { $('#btnSun').click(function() { myFunction(); }); $.fn.myFunction = function() { alert('hi'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button> 

I also tried to follow

 $(document).ready(function() { $('#btnSun').click(function() { myFunction(); }); }); function myFunction() { alert('hi'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button> 

This does not work! Any idea where I'm wrong?

+49
function jquery user-defined
Mar 25 '10 at 23:27
source share
8 answers

If you want to call a regular function through a jQuery event, you can do it like this:

 $(document).ready(function() { $('#btnSun').click(myFunction); }); function myFunction() { alert('hi'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button> 
+60
Mar 26 '10 at 0:29
source share

Just try this one. He always works.

 $(document).ready(function() { $('#btnSun').click(function() { $.fn.myFunction(); }); $.fn.myFunction = function() { alert('hi'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button> 
+27
Dec 07 '10 at 12:30
source share

They are called plugins, as Jaboc commented. To make sense, the plugin function must do something with the element to which it is called. Consider the following:

 jQuery.fn.make_me_red = function() { return this.each(function() { this.style.color = 'red'; }); }; $('a').make_me_red(); 
+11
Mar 26 '10 at 0:03
source share

The following is the correct method

 $(document).ready(function() { $('#btnSun').click(function(){ $(this).myFunction(); }); $.fn.myFunction = function() { alert('hi'); } }); 
+8
Jan 11 '12 at 6:20
source share

Try $('div').myFunction();

This should work

 $(document).ready(function() { $('#btnSun').click(function(){ myFunction(); }); function myFunction() { alert('hi'); } 
+6
Mar 25 '10 at 23:30
source share
 jQuery.fn.make_me_red = function() { return this.each(function() { this.style.color = 'red'; }); }; $('a').make_me_red() // - instead of this you can use $(this).make_me_red() instead for better readability. 
+3
Mar 29 '10 at 9:35 a.m.
source share
 jQuery.fn.make_me_red = function() { alert($(this).attr('id')); $(this).siblings("#hello").toggle(); } $("#user_button").click(function(){ //$(this).siblings(".hello").make_me_red(); $(this).make_me_red(); $(this).addClass("active"); });​ 

Function declaration and callback in jQuery .

0
Aug 29 '12 at 12:20
source share
 $(document).ready(function() { $('#btnSun').click(function(){ myFunction(); }); $.fn.myFunction = function() { alert('hi'); }; }); 

Put '; 'after defining the function ...

0
Jul 15 '13 at 14:43
source share



All Articles