Skip to content Skip to sidebar Skip to footer

How To Get The Parent Div Id From The Anchor Child

If ALL DASHBOARD encounters an error&l

Solution 1:

Use parentNode to access on your id

<div class="wizard-rules"id="wizard-rule">If <a href="#" onclick="alert(this.parentNode.id)"id="dashboard-rule-2" class="highlight">ALL DASHBOARD</a> encounters an error</div>

Solution 2:

You can get parent ID like this:

window.onload = function(){
    var element = document.getElementById('child').parentNode;
    alert(element.id);
}

Or you can use JQuery to do that:

var id = $('child').parent().prop('id');

Solution 3:

1st method: The parent() method traverses to the immediate parent of each of the element in the DOM tree. Parent() travels a single level up the DOM.

$('a').click(function(){
  alert($(this).parent().prop("id"));
}); 

and remove the onclick from the Anchor tag in the HTML

2nd Method:

The parentNode property returns the parent node of the specified node, as a Node object. In your HTML add this.parentNode.id to onclick event of anchor element:

<a href="#" onclick="alert(this.parentNode.id)"id="dashboard-rule-2" class="highlight">ALL DASHBOARD</a>

Solution 4:

Please try this will work fine :

     <div class="wizard-rules"id="wizard-rule">If <a href="#" onclick="alert($(this).parent().attr('id'))"id="dashboard-rule-2" class="highlight">ALL DASHBOARD</a> encounters an error</div>

Post a Comment for "How To Get The Parent Div Id From The Anchor Child"