views:

109

answers:

5

Hi all,

This question is related to this post but I don't see how I can ask another question in the same question.

I need to know what $(this) this is. If I alert $(this) I get [object Object]. In other words when I alert($(this)) I need for the outcome to be a specific <li> tag so that I can do an IF ELSE statement when I roll off a specific link.

Thanks. James

+5  A: 

In a handler, this is the DOM element. So to get the tag, just use the tagName property. Likewise, you can get other properties like ID with this.id.

alert( this.tagName );
alert( this.id );

Or if you needed to test against a specific selector (I wasn't sure from your question), you can use jQuery's .is().

if( $(this).is('.someClass') {...

(Actually, in that simple case of testing for a class, you'd probably use .hasClass() instead.)

if( $(this).hasClass('someClass') {...

There may be more efficient ways. Depends on what you actually need to test for.


EDIT: From your comment, you want to check if a child <a> element has the class rpSelected.

Try this:

if( $(this).children('a.rpSelected').length == 0 ) {
       // remove the class
patrick dw
jQuery offers the scoping argument in selectors too so the example you have with .children() could be written as`if ( !$("a.rpSelected",this).length ) ...`
sanchothefat
@sanchothefat - True, but that is less efficient on two counts. First, jQuery has to run a bunch of tests before it [finally converts it](http://github.com/jquery/jquery/blob/1.4.2/src/core.js#L147) into `$(this).find("a.rpSelected")` and starts over. Second, because it uses `.find()` it is less efficient because it searches *all* descendant elements instead of just the immediate children.
patrick dw
Awesome! That worked, thanks Patrick
Sixfoot Studio
@Sixfoot - You're welcome. :o)
patrick dw
A: 

To get the first element out of a jQuery object, use an indexer:

var val = $(this);
...
var domElement = val[0]; //domElement == this (from above)

To get the HTML:

var val = $(this);
...
var html = val[0].html();

Edit:
Reading your comment, are you looking for a class inside the <li> element?

var isSelected = $(this).find('a.rpSelected').length > 0;
Greg
Why wrap it with jQuery, then pull it right back out? :o)
patrick dw
@patrick - I suppose that I'm assuming that (1) `$(this)` is assigned to a variable, (2) we are acting on that variable, and (3) in the current scope, `this` no longer the DOM element. Otherwise this would be a silly.
Greg
Hi Greg. What I need from that alert is <li><a class="rpSelected"><span><span> so that I can do a check that says, on mouse out if $(this) == <li><a class="rpSelected"><span><span> then keep the current background image it had.
Sixfoot Studio
@Sixfoot - See my edit
Greg
Just curious, in the above example, what is the difference between `val` and `value`? is `value` a keyword?
funkymushroom
@funkymushroom - My typo, thanks.
Greg
A: 

If you want to check what is the current object and its values, write this console.log(this); and open firebug > console.

Stewie
A: 

What you can use to see all attributes is a function similar to PHP's print_r function:

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

And to output it just use print_r($(this));

o15a3d4l11s2
A: 

Hi Sixfoot Studio,

If you just want to execute conditional statements by the tag name represented by the this keyword you can do achieve this with the following code.

if ( $(this).is('li') ) {
    ...
} else if ( $(this).is('ul') ) {
    ...
} else if ( $(this).is('a') ) {
    ...
}

Remember that the keyword this refers to to the "owner" of the function we're executing. So you need to be sure who is calling the function...

Rubens Mariuzzo