Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the id of clicked element using jquery?

Tags:

jquery

I want to get the id of the clicked element and then show divs that match this id. I am using the following code, and it's not working. Please help.

$(function () {  

var tabContainers = $('div.difContetform > div');
    $('div#head-nav ul a').click(function (event) { 
        $('div#head-nav ul a').removeClass('current');
        $(this).addClass('current');
        var current_id = $(this).attr("id");
        var targeted='DIV'+current_id;
        $(targeted).show();
        $(targeted:not).hide(); 
        // 
        return false; 
     })
});
like image 832
Yasir Avatar asked Nov 16 '25 13:11

Yasir


1 Answers

You want to use the right selector syntax to grab your divs by id, which is the string #id... Therefore:

 $('#'+targeted).show();
 $('something:not(#'+targeted+')').hide();    

EDIT: Looking at this again (double-take), you can't just hide everything that doesn't match, as it will hide your whole page. You'll need to make sure you're selecting only DIVs, but not the one you want to show. How that works depends on your page layout (hence the something in the example above).

like image 176
Adam Bellaire Avatar answered Nov 19 '25 09:11

Adam Bellaire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!