5

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!

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
mauguerra
  • 3,728
  • 5
  • 31
  • 37

5 Answers5

4

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.

Naterade
  • 2,635
  • 8
  • 34
  • 54
  • [Here](https://stackoverflow.com/a/9440536/199364) is a code example of the jquery class-name approach. To clarify the first sentence and code snippet. (Would be useful to also have code example for the remaining paragraphs of this answer.) – ToolmakerSteve Jul 15 '20 at 18:42
3
<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>
ianaz
  • 2,490
  • 3
  • 28
  • 37
2

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 ?

Community
  • 1
  • 1
user1084509
  • 1,832
  • 10
  • 33
  • 48
1

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..)

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Punit
  • 1,110
  • 1
  • 8
  • 14