Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery nodename returning undefined

This code isn't for anything in particular. I'm just trying to successfully get the tagName or nodeName of an element. However, when I run the following code, I always get an alert saying "undefined". I'm wondering if it's because this function executes when the document is ready? Is there a different place I should be doing this? Or is it probably my other javascript code conflicting somehow (I would doubt).

 $(document).ready(function(){
        $('#first').hover(function() {
            alert($('#last').nodeName);
        });
    });
like image 834
Matthew Avatar asked May 05 '10 02:05

Matthew


2 Answers

Use the prop() of jQuery:

alert($('#last').prop("nodeName"));
like image 168
steven Avatar answered Oct 25 '22 12:10

steven


You are trying to access a non-member of the jQuery object. Use one of these DOM element accessors to retrieve these properties:

$( '#last' ).get(0).nodeName

OR

$( '#last' )[0].nodeName

OR

document.getElementById( 'last' ).nodeName

like image 44
Jacob Relkin Avatar answered Oct 25 '22 12:10

Jacob Relkin