jquery - Get the closest className -


i'm trying closest anchor classname that's clicked, however, not every link contain class therefore parent class runner up. classname different in many cases hard coding class names not expected.

i'm trying identify location of duplicate link class name see 1 performs better.

$("a").click(function() {     if ($("a[href='" + $(this).attr("href") + "']").length > 1) {         thisattr = $(this).closest().attr('class');         alert('\nclass = ' + thisattr + ' \n\nyes, duplicate link\n\n');         return false;     } else {         alert('\nno, not duplicate link\n\n');         return false;     } });   <div id="navigation">     <div class="utility">         <div><a href="http://www.google.com">google</a> (google not dup)</div>         <br>         <div class="test"><a href="http://www.example.com">example</a> (example duplicate)</div>         <br>         <div><a href="http://www.amazon.com">amazon</a> (amazon dup)</div>         <br>         <div><a href="http://www.amazon.com">amazon</a> (amazon dup)</div>         <br>         <div class="hello"><a href="http://www.intranet.com">intranet</a> (intranet not dup)</div>         <br>         <div class="world"><a href="http://www.example.com">example</a> (example dup)</div>         <br>         <div><a href="http://www.example.com">example</a> (example dup)</div>         <br>     </div> </div> </div> 

http://jsfiddle.net/no4gkk0n/12/

basically need specify element need find. closest return first ancestor of selected element.

thisattr = $(this).closest().attr('class'); 

in case calling closest without selector. means not specifying closest element trying catch. should pass selector or element along closest function.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -