Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a function in jquery

I'm a programming newbie, and I can't figure out how to store a function in JQuery and run in it multiple places.

I have:

$(function () {   $("div.class").click(function(){     //Doo something    });    $("div.secondclass").click(function(){     //Doo something   });  }); 

Now the 2 "//Doo somethings" are the same, and I don't want to write the same code again.

If I put:

$(function () {    function doosomething ()   {     //Doo something   }    $("div.class").click(doosomething);    $("div.secondclass").click(doosomething);  }); 

That would run the function on page load, rather than only when it clicks.

How do I do this correctly?

Thanks!

like image 257
Corin Avatar asked Jul 28 '09 03:07

Corin


People also ask

How can I call function in jQuery?

You need to use the script tag. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready. If you want an event to work on your page, you should call it inside the $(document).

Can we use function in jQuery?

In jQuery we can assign function in custom namespace. We can add or remove the function from namespace. This mechanism provide the functionality of storing the related function in a namespace.

How do I run one function after another in jQuery?

function Typer(callback) { var srcText = 'EXAMPLE '; var i = 0; var result = srcText[i]; var interval = setInterval(function() { if(i == srcText. length - 1) { clearInterval(interval); callback(); return; } i++; result += srcText[i]. replace("\n", "<br />"); $("#message").

Can jQuery call a JavaScript function?

The way to call a JavaScript function from a JQuery file is the same as calling a JavaScript function from a JavaScript file :) This is so because JQuery is a library based from JavaScript.


Video Answer


2 Answers

The following should work nicely.

$(function() {    // Way 1   function doosomething()   {     //Doo something   }    // Way 2, equivalent to Way 1   var doosomething = function() {     // Doo something   }    $("div.class").click(doosomething);    $("div.secondclass").click(doosomething);  }); 

Basically, you are declaring your function in the same scope as your are using it (JavaScript uses Closures to determine scope).

Now, since functions in JavaScript behave like any other object, you can simply assign doosomething as the function to call on click by using .click(doosomething);

Your function will not execute until you call it using doosomething() (doosomething without the () refers to the function but doesn't call it) or another function calls in (in this case, the click handler).

like image 175
Andrew Moore Avatar answered Oct 21 '22 10:10

Andrew Moore


I would do it this way:

(function($) { jQuery.fn.doSomething = function() {    return this.each(function() {       var $this = $(this);        $this.click(function(event) {          event.preventDefault();          // Your function goes here       });    }); }; })(jQuery); 

Then on document ready you can do stuff like this:

$(document).ready(function() {    $('#div1').doSomething();    $('#div2').doSomething(); }); 
like image 38
noboruwatanabe Avatar answered Oct 21 '22 09:10

noboruwatanabe