Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector reference to self

Tags:

jquery

Simple question:

How do I refer to self without using $.each() for example when I want to set html() to own attribute?

$('*[rel]').html($(this).attr('rel')); // nope
$('*[rel]').html($(self).attr('rel')); // nah
$('*[rel]').html($(sender).attr('rel')); // undefined
$('*[rel]').html($(target).attr('rel')); // still undefined
$('*[rel]').html($(???).attr('rel')); // this isn't even correct syntax
$('*[rel]').html($(beer).attr('rel')); // well that would be tasty
$('*[rel]').html($(woman).attr('rel')); // not in this life
$('*[rel]').html($(god help me!).attr('rel')); // He probably doesn't even know how to use a computer
$('*[rel]').html($(i give up).attr('rel')); // unexpected... o'rly?
like image 955
Flash Thunder Avatar asked Sep 16 '13 18:09

Flash Thunder


People also ask

What is the $() in jQuery ()?

When a jQuery object is passed to the $() function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.

What is Self in jQuery?

self can be used to still change the this that now reffers to the this from the inner function. var jquery_element= $(html_element); just provides a easy way to reference the jQuery element without having to constantly recreate it (also provides performance benefit for instance explained here). self.

How jQuery selectors are executed?

If dealing with more than two selectors in a row then your last selectors are always executed first. For example, jQuery will first find all the elements with class “. list” and then it will select all the elements with the id “second”. What is the Vibration API in HTML5 ?

What does a jQuery selector return?

The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example).


1 Answers

You can pass a function instead of a a string.

$('[rel]').html(function() {
    return $(this).attr('rel');
});

The reason why all of your examples don't work is because in each case your code executes long before the selector has been evaluated. This is something you really need to understand - it's even more relevant when callbacks are involved e.g. for AJAX or timers: When you put code somewhere it's executed at this point. So foo(some code here) will always execute the code that's part of the function argument no matter what foo does. The only reason to avoid this is passing a function that contains this code - that was the function expression is evaluated (which evaluates to a callable function). Then the actual function (which obviously needs to expect a callable instead of a simple value) can invoke the passed function whenever it's appropriate.

like image 76
ThiefMaster Avatar answered Nov 15 '22 22:11

ThiefMaster