javascript - cannot get id of parent -


i trying id of event targets parents parent, comes undefined, know fact id exist have used firebug confirm this.

here html markup:

<div class="grid-stack-item ui-draggable ui-resizable ui-resizable-autohide" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="4">         <div class="grid-stack-item-content ui-draggable-handle" id="chart"></div>         <div class="overlayed" data-render="" data-charttype="" data-devid="" data-metrictype="" data-refresh="30000" data-title="" data-subtitle=""                     data-yaxis="" data-tooltip="" data-linewidth="0.5" data-timewindow="10">             <span class="glyphicon glyphicon-pencil panel-icons chart-edit" style="padding-right: 10px"></span>             <span class="glyphicon glyphicon-zoom-in panel-icons"></span>             <span class="glyphicon glyphicon-trash panel-icons"></span>         </div>     </div> 

and here javascript/jquery trying id

$('.overlayed').on('click', '.glyphicon-pencil', function (e) {     e.preventdefault();     e.stoppropagation();     alert("what going on!");     //hold overlayed element later use     var overlayeddata = $(this);     var chartid = $(this).parent().parent().attr('id');      $('#editwidgetmodal').modal('toggle'); } 

the parent().parent() of .glyphicon-pencil .grid-stack-item element, has no id. assume you're attempting target #chart element, seen it's 1 id in html sample. if case, code fails element sibling of parent element. in case can use prev() it. try this:

var chartid = $(this).parent().prev().prop('id'); 

or:

var chartid = $(this).closest('.overlayed').prev().prop('id'); 

working example


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -