If I would have a simple function like this:

jQuery.fn.example = function(){
   this.remove();
}

and call it with an empty selector:

$().example();

What element would then this be pointing to? Or is there a way to make a function able to be executed through both $.example(), without a selector, and with $(something).example()? If so, how can I find out which way the function has been executed so I can have different actions depending way of execution?

Recommended Answers

All 5 Replies

Basically, "this" refers to the selected element you have chosen to remove. Let's say, if you have called the .example() function like ( $("a").example(); ), "this" will refer to all link elements in your page. So basically, I think you don't have to call the $.example() function if you are not pertaining to any particular element.

What I meant was what would happen if a script dependent of a selector would be called without a selector used, what would then be automatically selected? The jQuery object or the document object perhaps? Do you understand my question?

Yeah, I do understand your question. That's a good question, but I never encountered that kind of situation yet so basically, I don't have any idea about it.

Ok :) thanks anyway, if you ever find out, let me know will you?

OK, I will.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.