javascript - Getting parent element in jQuery -


i have tried parent element's id of clicked element. have tried this

$(window).click(function(event) {     alert("clicked on " + event.target.id + ". parent " + $(event.target).parent().attr("id")); }); 

if have following structure, returns clicked on x. parent undefined.

<div id="y">     <div id="x">x</div> </div> 

why isn't giving parent's id?

i have tried following too

$(this).closest("div").attr("id"); $(this).parent().attr('id') 

$(document).on('click', '.clicked-element', function(){     parentelid = $(this).parent().attr('id');    // parentelid = $(this).parents('.custom-parent-class').attr('id'); // if wan't id different parent }); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -