Piotr Wu
Piotr Wu

Reputation: 1362

Get ancestor onclick attribute

I'm writing script which should find onclick attributes on any ancestor.

For example when I click on span.tabIdent I want get anchor onclick (I trigger it later)

HTML:

<a class="t3 lin tab-B" rel="#categoriesLinux" rev="lin" onclick="changeTab()">
  <span class="tabLeft" style=""></span>
  <span class="tabCenter" style="">
    <span class="tabIdent" style=""></span>
  </span>
  <span class="tabRight"></span>
</a>

JS:

var foo, 
    foo2,
    przodek,
    parentsCount = jQuery(that).parents().length,
    przodek = jQuery(that); //it's ok here

for(var ii=0; ii<parentsCount; ii++){
  przodek = jQuery(przodek).parent();
  foo = jQuery(przodek).prop('nodeName'),
  foo2 = jQuery(foo).attr('onlick');
  console.log(foo+' : '+foo2);
}

It return everywhere 'undefined'. What is wrong with this ?

Upvotes: 0

Views: 148

Answers (2)

Chris Dixon
Chris Dixon

Reputation: 9167

You could simply do this...

$('.tabIndent').on('click', function() {
   var anchor = $(this).closest('a');

   console.log(anchor.prop('nodeName') + ' : ' + anchor.prop('onclick'));
});

Upvotes: 0

Quentin
Quentin

Reputation: 943981

You make a spelling error: onlick is not onclick

Upvotes: 2

Related Questions