4 Replies - 42009 Views - Last Post: 16 November 2011 - 05:25 PM

#1 FALL3N1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 11

Calling a function from within another function.

Posted 11 November 2011 - 01:00 AM

Hey.. I'm pretty new to JQuery.. like, very new actually.. I know this is an extremely simple question if you are not a nooblet at JQuery, but I gotta ask it so I can move on.

How do I call a method from another method. I know, if your thinkin, "is he kidding? That's like one of the first things I learned." then it is probably the answer im looking for.

For example (but in javascript instead, cuz I know how to do it):

function partA() {
    partB();
}


yeah, easiest question ever.. it'll only take a second to answer, and will be very helpful to me! Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a function from within another function.

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Calling a function from within another function.

Posted 11 November 2011 - 03:28 AM

Hey.

jQuery is just a Javascript library. It adds features to Javascript, but all the syntax and rules that normally apply to Javascript also apply when jQuery is being used. So the code you posted is how you call a function from within a function in jQuery. It's the same.


Or am I perhaps misunderstanding the question?
Could you perhaps give us an example of what you're thinking? Even if it doesn't work it may help us understand.
Was This Post Helpful? 1
  • +
  • -

#3 FALL3N1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 11

Re: Calling a function from within another function.

Posted 11 November 2011 - 02:56 PM

View PostAtli, on 11 November 2011 - 03:28 AM, said:

Hey.

jQuery is just a Javascript library. It adds features to Javascript, but all the syntax and rules that normally apply to Javascript also apply when jQuery is being used. So the code you posted is how you call a function from within a function in jQuery. It's the same.


Or am I perhaps misunderstanding the question?
Could you perhaps give us an example of what you're thinking? Even if it doesn't work it may help us understand.



Kinda... sry but u answered like half the question, and cleared up some other questions I had, thanks for that.

But I was just wondering in the above code, partA() calls partB().. these are written
function partA() {
   partB();
}

function partB() {
 code....
}



but in this javascript example, the functions are named partA and partB.. but in JQuery, the functions have no names.. they are like:
$("p").click(function () { ..code...}
so how would I call this function from another one? The first answer I can think of is, because you said JQuery was a javascript library that adds functionality to it, it seems like the answer would be that javascript code called onclick for a button with id="theButton" would be executed aswell as the JQuery function for $("#theButton").click() .. but what if a JQuery method is not called by a specific user action, like a button click, like an auxiliary method that is used by other methods to slightly alter their output... like a javascript checkForFirstLetterCapitalized(var rawInput) that is used by both getStudentFirstName() and getStudentLastName()?

does JQuery not allow such methods? does every JQuery method have to be triggered by a user action?
Was This Post Helpful? 0
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Calling a function from within another function.

Posted 11 November 2011 - 11:06 PM

What you are defining with that jQuery code is an event, using an anonymous function as a callback. Events are triggered by user action, or for a specific "event" in an object. When they are triggered a callback function is called, which you set as you did in your jQuery code.

There are basically two ways to define a callback function. - This goes for both jQuery and Javascript syntax. (Remember, jQuery is just an addition to Javascript; it follows the same rules.)

The first is by attaching it to an identifier:
function theFunction() { ... }

// Or, alternativly
var theFunction = function() { ... }


This way you can refer to this function later by name, either to call it manually or to set it as the callback for events.
// The normal Javascript way to set an event
var p = document.getElementById("theP");
p.addEventListener("click", theFunction, true);

// And the jQuery equivalent:
$("#theP").click(theFunction);

// The above line is a short-hand for this:
$("#theP").bind("click", theFunction);


Now every time the "click" event is triggered for the "theP" element, theFunction is called. This does not have to be an actual mouse click by the user. You can trigger this event manually by doing:
$("#theP").click();

// Which is equivalent to:
$("#theP").trigger("click");




The second way to do this is by using an anonymous function, like you did in your code. Anonymous functions are functions not tied to a name in the current scope and are thus not callable. - Keep in mind that in Javascript pretty much everything is an object. That includes functions. This means you can pass a function around just like any other object; sending it as a function parameter if you so choose.

They are handy when you don't actually need to call it in your code; when passing it as a callback is it's only purpose. If that is not the case; when you actually do need it for another purpose, you should give it a name and pass "normally", as I explained above.

So, creating an anonymous function is exactly like creating a normal function, except without the identifier, and you do it directly in the function you're passing it into:
// Normal Javascript
var p = document.getElementById("theP");
p.addEventListener("click", function() {
    // Do stuff when the user clicks "theP"
}, true);

// jQuery
$("#theP").click(function() {
    // Do stuff when the user clicks "theP"
});


The function will now be called when the "click" event is triggered for "theP", but you can not call the functions manually from the code (without triggering the event.)
Was This Post Helpful? 4
  • +
  • -

#5 FALL3N1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 11

Re: Calling a function from within another function.

Posted 16 November 2011 - 05:25 PM

omg, thank you so much for that incredibly good and full explanation!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1