-3

I had my HTML like this.

<ul>
   <li><a href="#" id="abc"></a></li>
   <li><a href="#" id="bca"></a></li>
   <li><a href="#" id="cab"></a></li>
   <li><a href="#" id="acb"></a></li></ul>

when the user clicks on a particular link its id should be passed to an ajax script without using an onclick() function in markup HTML.Can it be achieved using the same anchor elemnt?? Thanks in advance.

1
  • look at click() Commented Jul 10, 2014 at 11:21

2 Answers 2

2

using jquery click event

  $("ul li a").click(function(e){
    e.preventDefault();
    var id = this.id;
// call your ajax code

    });

Bind an event handler to the "click" JavaScript event, or trigger that event on an element.

Sign up to request clarification or add additional context in comments.

1 Comment

But how to get id of the anchor??
-1
$("ul li a").click(function(e){
var anchorid = $(this).attr('id');
alert(id);
e.preventDefault();
// call your ajax code

});

You will get id in anchorid variable.

2 Comments

give me reason of vote down please
Not my downvote but you probably got it when you only included a link. Link-only answers are downvoted/flagged by SO users and removed by moderators.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.