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!
AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.
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. Here is a code example of the jquery class-name approach. To clarify the first sentence and code snippet.
You would place your JavaScript in the <head> or the <body> . However, putting all of your JavaScript includes and JavaScripts at the bottom of the <body> section is best for loading performance.
ajax(url,[options]); Parameter description: url: A string URL to which you want to submit or retrieve the data. options: Configuration options for Ajax request. An options parameter can be specified using JSON format.
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.
<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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With