-1

How can i get Parent Div id of component.

I have already referred this link but its not working.

jQuery get id of element by searching for it by class

Here Is my Sample Code.

<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
            function linkClickEvent()
            {
                alert ($(this).parent().attr('id')); //Gives Me Undefined
                alert($(this).closest('div').attr('id')); //Gives Me Undefined
                alert($(this).parent().attr('id')); //Gives Me Undefined
            }
        </script>
    </head>
    <body>
        <div id="container_1">
            <a href="#" onclick="linkClickEvent();" id="link_1">Container_1</a>
        </div>
        <div id="container_2">
            <a href="#" onclick="linkClickEvent();" id="link_2">Container_2</a>
        </div>
        <div id="container_3">
            <a href="#" onclick="linkClickEvent();" id="link_3">Container_3</a>
        </div>
    </body>
</html>
Community
  • 1
  • 1
MRX
  • 1,611
  • 8
  • 33
  • 55

5 Answers5

7

Change your onclick event, in onclick event function linkClickEvent pass current element reference using this :

 <a href="#" onclick="linkClickEvent(this);" id="link_1">Container_1</a>

and in function add parameter to accept element reference:

function linkClickEvent (element) {
    console.log($(element).closest("div").attr("id"));
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
4

The issue is because you are using on* attributes to hook up your events, this refers to the window, not the clicked element. Use jQuery to hook up your events:

<div id="container_1">
    <a href="#" id="link_1">Container_1</a>
</div>
<div id="container_2">
    <a href="#" id="link_2">Container_2</a>
</div>
<div id="container_3">
    <a href="#" id="link_3">Container_3</a>
</div>
$('div a').click(function(e) {
    e.preventDefault();
    var parentId = $(this).closest('div').prop('id');
    alert(parentId);
});

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You should do event handling in your JS files. Separating content, style and behaviour really is best practice.

You can get your element via:

$(document).on('click', '.element', function(){/*...*/});

And then save the id of the attribute clicked via:

var parent_id = $(this).parents('.classOfParent').attr('id');

or

var parent_id = $(this).parent().attr('id');

Also, if in doubt, check what "this" is with console.log() which works WAY better than alert().

Edit: Revised your question. Sorry, now it's correct.

PS: Why is console.log() considered better than alert()?

Community
  • 1
  • 1
CunningFatalist
  • 453
  • 1
  • 9
  • 21
0

Use event.preventDefault();

$("a").click(function(event) {
 event.preventDefault();
  $(this).parent().attr('id');
});
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
0

Use document.ready instead of onclick attribute.

 $(document).ready(function() {
            $('a').click(function() {
                var id = $(this).parent().attr('id');
                alert(id);
            });
        });
Sharique
  • 4,199
  • 6
  • 36
  • 54