-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
  • 1,031,962
  • 187
  • 1,923
  • 1,875
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
  • 9,000
  • 1
  • 23
  • 40
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
  • 26,047
  • 12
  • 43
  • 63