Reputation: 1
Is there a way to make an Ajax call from a hyperlink?
I'd like to change the inner html of a div element when someone clicks on a hyperlink using javascript.
Upvotes: 0
Views: 990
Reputation: 943979
Ajax is just "something you can do with JavaScript"
Hyperlinks can have event handlers bound on them like any other HTML element.
var links_in_document = document.getElementsByTagName('a');
var a_link = links_in_document[0];
a_link.addEventListener('click', a_function_that_does_something_ajaxy);
function a_function_that_does_something_ajaxy(e) {
var xhr = new XMLHttpRequest();
// etc
e.preventDefault();
}
Note: No compatibility routines for browsers that don't support standards are included in this code sample. You should add some (along with a more sensible way to determine which link you are targeting).
Upvotes: 2