mauguerra
mauguerra

Reputation: 3858

HTML link with Ajax

I have 6 different links,and each link is going to call a different Ajax function.

I'm using the <a href> tag because I want it to appear as a link....Can I use this tag to call the Ajax function? or it only works with URL links?

THANKS!

Upvotes: 5

Views: 49618

Answers (5)

Punit
Punit

Reputation: 1120

yes you can, if any client side script functions (javascript or jquery) applied than it will execute first.

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195972

You can extract the value of the href attribute and use it for your AJAX call...

(it is actually the proposed way to handle it..)

Upvotes: 1

user1084509
user1084509

Reputation: 1902

Yes you can incorpore the link in the following way: - on your link you can write <a href="#" onclick="function()">...</a>

Here you can see further information about this topic:

What is the difference between the different methods of putting JavaScript code in an ?

Upvotes: 2

ianaz
ianaz

Reputation: 2580

<a href="#" onclick="function()">Text</a>

or even as they wrote, with jquery

<a href="#" id="blabla">Text</a>

<script type="text/javascript">
$(document).load(function(){
  $('#blabla').click(function(){
  alert("Clicked");
  });
});
</script>

Upvotes: 3

Naterade
Naterade

Reputation: 2675

This is how I call mine. I give my elements a class name such as 'clickable' then use Jquery's click function as so.

$('.clickable').click(function() {
   //do ajax
});

Then in the function, I get the id of the element as so. var id = this.id, this will get the unique id of the element.

After that I use the $.post method of Jquery, the shorthand version of ajax and complete whatever call you need to make when the user clicks that link using the id.

Of course, in my case I never use the anchor tag, I just make is a button or apply the . click to the element I wish to add the ajax call to, but you could just surround the "link" in a span or a div to simulate the same effect.

Hope this helps in some way or another.

Upvotes: 4

Related Questions