2

I've got this function which runs for each instance of 'fullName':

$(xml).find("fullName").each(function(){ ... });

But there is really only ever one instance and I don't want to be using each, is there some other way to create this function without using each. Maybe just:

$(xml).find("fullName").it(function(){ ... });
6
  • Yes, use first(), or explain what function it is, and there might be a suitable jQuery method to use. Commented Jun 14, 2013 at 14:58
  • I mean, the function is rather unique, but does jQuery have a method for setting/ getting cookies? Commented Jun 14, 2013 at 15:00
  • This has been discussed here: stackoverflow.com/questions/4856938/…. Commented Jun 14, 2013 at 15:01
  • Not without a plugin, I think, no. Commented Jun 14, 2013 at 15:01
  • It might depend on what the Function is meant to be used for.. Ex: Do you wanna use a Simpe function expression or a whole function declaration... There might be several alternatives to achieving what you want.... Just try to Explain further Commented Jun 14, 2013 at 15:02

6 Answers 6

4

You can try something like this

var func = function(){ ... }
func.call($(xml).find("fullName")[0], args));
Sign up to request clarification or add additional context in comments.

Comments

3

You could just reference by index:

$(xml).find("fullName")[0]

2 Comments

But where is the function?
@Watson As the value you wish to use is directly accessible there is no need for a function. Of course, you could use a function if you liked. This is described by LimH's answer.
1

Have a look at the .first() method

http://api.jquery.com/first/

1 Comment

But how does that help the OP run a function? That just returns another jQuery object and he or she already has a jQuery object...
1
var fnc = null;
if (typeof (fnc = $(xml).find("fullName").eq(0)) == 'function'){
    fnc(args);
}

4 Comments

But how does that help the OP run a function? That just returns another jQuery object and he or she already has a jQuery object...
@nnnnnn, add .call(arguments)
So that's what your answer should say. With more detail. (Assuming it doesn't then become a duplicate of Lim H.'s answer.) But you don't need .eq(0) since the OP is confident that the .find() will only find one item anyway - that's the point of the question...
@nnnnnn, I used to not trust the customer's data. Answer corrected.
0

What of:

(function($){
 $.fn.anotherFunction=function(){

alert('Handles for anotherFunction');
}
})(jQuery)

and call it like::

$(xml).find("fullName").anotherFunction();

is that what He Wants??

Comments

0

Or....

$(xml).find("fullName").first()

1 Comment

But (as I already asked about some other answers) how does that help the OP run a function? That just returns another jQuery object and he or she already has a jQuery object...

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.