Nisarg Bhavsar
Nisarg Bhavsar

Reputation: 956

get link attribute id using jquery

I am working in wordpress.I want to get data-id value when click on link.

My code is like this:

<?php

    $query3 = "select t.* from wp_terms as t 
              join wp_term_taxonomy as tt on tt.term_id = t.term_id
              where tt.parent = ".$res2->term_id;
              $result3 = $wpdb->get_results($query3);


    foreach($result3 as $res3)
    {
?>
        <li>
            <a href="##" class="sub_cat_name" data-id= "<?php echo $res3->term_id; ?>"><strong><?php echo $res3->name; ?></strong></a>
        </li>
<?php
     }
?>

And my jQuery code is:

<script>
 $(document).ready(function(){
    $(".sub_cat_name").click(function(){
        var cat_id = $(".sub_cat_name").attr("data-id");
        alert(cat_id);
        });
    });
</script> 

When I click on different link then it alert only fist term_id.So what code should I have to write to get different id while click on different link.

Upvotes: 0

Views: 118

Answers (1)

Renish Khunt
Renish Khunt

Reputation: 5824

you can get data-id value using 'data' like.

$(".sub_cat_name").click(function(){
    var cat_id = $(this).data("id");
    alert(cat_id);
    });

Upvotes: 1

Related Questions