-2

How can I get id of anchor tag specified in li tag?

My code is:

 <li name="cmspage"><a href="#" id ="<?php echo $data ->page_id; ?>"><?php echo $data ->page_title; ?></a></li>
Vyomesh Vora
  • 51
  • 2
  • 8

3 Answers3

1

From what little you've shown in your question, it would be:

var id = $("li[name=cmspage] > a").attr("id");

More in the CSS selectors spec and the jQuery API.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
0

You need to select li tag together with anchor as following code :

<script>
$(function(){
   var id = $('li a').attr('id');
   alert(id);
});
</script>
Norlihazmey Ghazali
  • 8,762
  • 1
  • 19
  • 37
0

With $("li a").attr("id") :

alert( $("li a").attr("id") )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul><li name="cmspage"><a href="#" id ="id1">Some title</a></li></ul>
Jeremy Thille
  • 21,780
  • 7
  • 36
  • 54