Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't javascript/jQuery get parentNode?

Considering that I have an element like this:

<p class="ELP">2/1/2013 - <a id="EL3" class="ELLink" href="/Event htms/Event.cshtml?title=Okmulgee Public Schools County Professional Day&amp;explanation=Okmulgee Public Schools County Professional Day.&amp;dateString=2-1-2013">City Public Schools County Professional Day</a></p>

Why does this JavaScript/jQuery work...:

$(".ELP").click(function (){
        var stringDate = $(this).text().substring(0, 8)
        console.log(stringDate)
    });

Console.log produces: 2/1/2013

...And this JavaScript/jQuery doesn't?

$(".ELLink").click(function (){
        var stringDate = $(this).parentNode.text().substring(0, 8)
        console.log(stringDate)
    });

Console.log produces nothing because of JavaScript error: Uncaught TypeError: Cannot call method 'text' of undefined

Clearly I can see that it is not 'getting' the right element, but why? Isn't the first parent node of my 'a' element the 'p' element? As far as I understand it, there is no (at least no cross-browser/platform compatible) valid css selector for the direct parent node of an element or i would just use that.

What am I not seeing?

like image 577
VoidKing Avatar asked Dec 21 '25 09:12

VoidKing


1 Answers

The jQuery method is called parent()

$(".ELLink").click(function (){
        var stringDate = $(this).parent().text().substring(0, 8)
        console.log(stringDate)
    });
like image 115
Ibu Avatar answered Dec 23 '25 01:12

Ibu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!