Reputation: 432
I have made some search but have yet not found the solution. I want to load a page with jquery load by clinking on a link with rel but for the update, i want the jquery to locate the data in the link on which i have clicked and update the id of that data .
Question : Instead of updating id_du_milieu i want the script to locate data-ajax="id_to_update" and update the div having the value of data-ajax which is id_to_update when you click on link one and update_id_2 when you click on link 2.
I do not want to add update_id_2 or id_to_update in the javascript but i want a jquery function that will take the value of data-ajax as id to update
This is my initial code
<a href="churches.php" rel="charger_tout" data-ajax="id_to_update">Link 1</a>
<div id=id_to_update></div> <a href="churches.php" rel="charger_tout" data-ajax="update_id_2">Link 2</a>
<div id=update_id_2></div>
Using
$('a[rel*=charger_tout]').on('click', function () {
$('#id_du_milieu').html('<img src="images/facebook_style_loader.gif" />');
$('#id_du_milieu').load($(this).attr('href'), function (responseText, textStatus, XMLHttpRequest) {
if (textStatus == 'error') {
$('#id_du_milieu').html('<p>There was an error making the AJAX request</p>');
}
});
return false;
});
Upvotes: 0
Views: 123
Reputation: 754
There is nothing wrong with using variable inside a selector, so you can do:
var myId = $(this).data('ajax');
$('#'+myId).html(/* insert some stuff here */);
On the other hand, if you want to address an element with data attribute (you didn't make that clear or I didn't understand well) you can do:
var myId = $(this).data('ajax');
$('a[data-ajax="+myId+"]').html(/* insert some stuff here */);
Instead of using jQuerys $(this).data('ajax')
function you can also use $(this).attr('data-ajax')
and that will equal the same. Data attributes are widely supported so unless you care about HTML validation you can use them even in IE4.
Upvotes: 2