Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML link with Ajax

Tags:

html

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!

like image 869
mauguerra Avatar asked Dec 12 '11 16:12

mauguerra


People also ask

Does AJAX work with HTML?

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.

How do I add a link in AJAX?

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.

Where do I put AJAX code in HTML?

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.

What is an AJAX URL?

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.


2 Answers

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.

like image 73
Naterade Avatar answered Nov 15 '22 06:11

Naterade


<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>
like image 38
ianaz Avatar answered Nov 15 '22 08:11

ianaz