3

How can I achieve this?

$(".my_selected .element").myCustomFunction();

or maybe even simply this?

$(".my_selected .element").???????(myCustomFunction());

Where the function will know that $(this) equals $(".my_selected .element").

Thank you!

2

5 Answers 5

4
$.fn.myCustomFunction = function(){
    // this or $(this) is $(".my_selected .element")

}
Sign up to request clarification or add additional context in comments.

9 Comments

this is $('.my_selected .element').
@FelixKling: heh.visited your profile with unicorns plugin -> result[1, 2]
don't forget the semicolon at the end : )
No need for $(this) as this has already been wrapped in a jquery object for you, if i understand the documentation right.
@Mohammad: it's not necessary
|
3

Write a plugin: http://docs.jquery.com/Plugins/Authoring

Comments

1

You extend jQuery's functionality by adding your own function as so:

jQuery.fn.myCustomFunction = function() {

};

The this keyword works as intended within the function if I remember correctly.

Comments

1

Use the code below, see a working demo here

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

    alert($(this).html());

  };
})( jQuery );

$(".my_selected .element").myCustomFunction();

It is the simple way of extending jQuery

Comments

1

Sounds like you want the each function if you aren't talking about actually writing a jQuery plugin. If you know all of your items returned from the selector support the function you could just do something like.

$('li').each(function(index) {
    alert(index + ': ' + $(this).text());
});

Additional information about this jQuery approach http://api.jquery.com/each/

Comments

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.