3

Possible Duplicate:
Can jQuery provide the tag name?

I want to display text inside the div tag in two different situations:

  1. When I hover over the first child element I want to see text as id and the name of the tag element.
  2. When I hover over the other elements (not first child) I want to see the text "this not first child".

Example

HTML:

<ul id="ul1">ul1
   <li id="aa">1</li>
   <li id="bb">2</li>
   <li id="cc"class="elm">3</li>
   <li id="dd">4</li>
   <li id="ee">5</li>
</ul>
<ul id="ul2">ul2
   <li id="ul2-1">1</li>
   <li>2</li>
   <li>3</li>
   <li>4</li>
   <li>5</li>
</ul><br/>

<div>element-name</div>

CSS:

div,li{ width:40px; height:40px; margin:10px;
        float:left; border:2px blue solid; 
        padding:2px; }
ul{ width:100px; height:10px; margin:10px;
        float:left; border:2px blue solid; 
        padding:2px; }
  span { font-size:14px; }
  p { clear:left; margin:10px }​  

jQuery:

var $curr = $(".elm");
$curr.css("background", "#f99");

$("*:first-child")
        .css("background", "#cac")
        .hover(function () {
              $(this).css("background","green");

            $('div').text(this.id+$(this).tagname());

            }, function () {
              $(this).css("background", "#cac");
            });
Community
  • 1
  • 1
centerwow.com
  • 67
  • 1
  • 7

1 Answers1

5

You can get the tag nme with the DOM property .tagName. This is NOT a jQuery property, it's a DOM property like this:

$("*:first-child").hover(function () {
    console.log(this.tagName);
});

You could make the id and tag name be the text of a div with this:

$('div').text(this.id+this.tagname);

If you already have a jQuery object and want to get the DOM object from it, you can do so with either .get(0) or [0] as in myObj$.get(0) or myObj$[0]. In your event handler, this is already the DOM object of the object that originated the event.

Beyond that, I can't tell what you're really asking for.

jfriend00
  • 683,504
  • 96
  • 985
  • 979