dorsaf dhouibi dorsaf dhouibi - 2 months ago 7
HTML Question

How can I get the immediate parent of HTML tag

This is the code:

<a href="">
<i class="fa fa-facebook-square cbutton--effect-marko cbutton--click"></i>
</a>


How can I get the
a
tag? When I use
.parent()


The result is:

Object { length: 0, prevObject: Object, context: HTMLDocument → index.html }

Answer

As stated in the comments you probably execute your JavaScript before your HTML finished loading. So there is no i or a element at that moment.

Either use the document.ready method from jQuery, here as shorthand:

$(function() {
    var parenTag = $('i').parent('a');
});

Or place your JavaScript code below your HTML code.

<a href="">
    <i class="fa fa-facebook-square cbutton--effect-marko cbutton--click"></i>
</a> 

<script>
    $(function() {
        var parenTag = $('i').parent('a');
    });
</script>