Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Intercept the link’s click, cancel its normal navigation with event.preventDefault(), then start an asynchronous $.ajax() request inside the handler. The URL, HTTP method, request data, and response type must match your server endpoint.
Basic click-to-Ajax pattern
Keep a real destination in href, then attach a handler with jQuery’s general .on() API. This example expects the endpoint to return JSON:
<a id="myLink" href="/api/items" class="ajax-link">Load items</a>
$('#myLink').on('click', function (event) {
event.preventDefault();
$.ajax({
url: $(this).attr('href'),
method: 'GET',
dataType: 'json'
})
.done(function (data) {
// Render or otherwise use the returned data.
console.log(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
// Show a useful failure state.
console.error(textStatus, errorThrown);
});
});
.on('click', handler) has been the general jQuery event-binding interface since jQuery 1.7. The handler receives a jQuery Event object, and in this direct binding this is the clicked anchor, so $(this).attr('href') reads its destination.
Why preventDefault() matters
A browser normally follows an anchor after a click. event.preventDefault() cancels that default navigation; it does not make an Ajax request by itself. The Ajax call must still be made explicitly in the handler.
#1 Best Overall
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Use the event object and call the method before the handler finishes:
$('#myLink').on('click', function (event) {
event.preventDefault();
// Start the replacement behavior here.
});
Returning false in a jQuery handler also prevents the default action and stops propagation. Prefer explicit preventDefault() when you only want to cancel navigation, because it communicates the narrower intent.
Rank #2
- JavaScript Jquery
- Introduces core programming concepts in JavaScript and jQuery
- Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
Align the Ajax settings with your endpoint
The sample uses asynchronous GET and expects JSON only as an illustration. Change each setting to the contract implemented by your server.
| Setting | What to verify |
|---|---|
url |
Use the endpoint that should receive the request. Reading the anchor’s href avoids duplicating the URL in JavaScript. |
method |
jQuery documents GET as the default, but the API may require another method. |
data |
Send query or request fields in the format your endpoint accepts. The example has no payload. |
dataType |
Set json only when the response is JSON. Otherwise remove it or select the response type your endpoint returns. |
Do not add authentication headers, CSRF fields, or a response schema without knowing the application’s requirements. jQuery requests are asynchronous by default. Avoid synchronous Ajax: it can lock the browser, and synchronous use with jqXHR/Deferred has been deprecated since jQuery 1.8.
Handle success and failure
Successful responses
The .done() callback receives the parsed response data, a status string, and the jqXHR object. Update the page, show the returned items, or perform the next application action there.
.done(function (data, textStatus, jqXHR) {
$('#results').text(JSON.stringify(data));
});
Failed responses
The .fail() callback receives the jqXHR object, a status category, and an optional error value. Use those values to present an actionable message instead of assuming that a click succeeded.
.fail(function (jqXHR, textStatus, errorThrown) {
$('#status').text('Request failed: ' + textStatus);
});
Common jQuery error categories include timeout, error, abort, and parsererror. A parser error often means the response could not be interpreted as the requested data type, such as invalid JSON when dataType: 'json' is set.
Use delegated handling for links added later
A direct binding applies only to elements selected when the binding runs. If your application inserts or replaces matching links after page load, bind to a stable nearby ancestor and provide a selector:
Best Value
$('#linkContainer').on('click', 'a.ajax-link', function (event) {
event.preventDefault();
$.ajax({
url: $(this).attr('href'),
method: 'GET',
dataType: 'json'
})
.done(function (data) {
// Render the response.
})
.fail(function (jqXHR, textStatus, errorThrown) {
// Show an error.
});
});
The ancestor receives the bubbled click and jQuery checks whether the event path matches a.ajax-link. Choose an ancestor close to the links when practical rather than delegating every handler from document; broad delegation requires more selector checks.
| Binding choice | Use it when |
|---|---|
| Direct | The link already exists and will remain in the page. |
| Delegated | Matching links can be inserted, removed, or replaced later. |
Implementation checks and troubleshooting
- Load jQuery before this initialization code runs, and confirm the selector matches the intended anchor.
- If the browser still navigates, verify that the handler receives
eventand callsevent.preventDefault()before it returns. - If a request fires twice, look for initialization code that binds the same handler repeatedly during page updates. Bind once from a stable initialization point; when a deliberate rebind is required, remove only the intended handler with
.off(). - If newly inserted links do nothing, switch from direct binding to delegation through a stable ancestor.
- If the request fails, inspect the browser’s Network response and the jqXHR status fields. A server error or malformed response is separate from whether the click handler ran.
Keep a meaningful fallback link
Leaving a useful href gives the anchor a real destination when JavaScript is unavailable and makes the target visible in the markup. The Ajax behavior can enhance that normal navigation rather than making the link unusable outside the scripted path.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




