Reputation: 3653
I want to replace all '20' inside the id="olderpost" with '30'
I did the following:
<div id="olderpost"><a onclick="$.ajax({ dataType: 'script', url: '/actions/olderpost/20'}); return false;" href="/actions/olderpost/20">View older actions</a></div>
$('#olderpost').html().replace('20', '30');
but nothing changed.
Upvotes: 0
Views: 360
Reputation: 185893
Your code is redundant - you have the same URL twice in the code. In order to remove this redundancy, I propose this initial state:
HTML:
<div id="olderpost">
<a href="/actions/olderpost/20">View older actions</a>
</div>
JavaScript:
$('#olderpost a').click(function(e) {
e.preventDefault();
$.ajax({dataType: 'script', url: $(this).attr('href')});
});
So, the URL is stored in the href
attribute of the anchor. Then, if JavaScript is enabled, the click handler will make an Ajax request and the URL for that request will be taken from that same href
attribute.
In order to replace the URL, just modify the href
attribute of the anchor:
$('#olderpost a').attr('href', function(i, v) {
return v.replace('20', '30');
});
Upvotes: 0
Reputation: 129001
replace
doesn't mutate the string. Try this:
$('#olderpost').html($('#olderpost').html().replace(/20/g, '30'));
Upvotes: 2