I am using the following jQuery code to enable users to vote thumbs up or down for each post:
$('#link0 a').click(function(){
var href=$(this).attr('href');
var querystring=href.slice(href.indexOf('?')+1);
$.post('rate.php', querystring, function(data){
var newHTML;
newHTML = data;
$('#link0').html(data);
});
return false; // stop the link
});
$('#link1 a').click(function(){
var href=$(this).attr('href');
var querystring=href.slice(href.indexOf('?')+1);
$.post('rate.php', querystring, function(data){
var newHTML;
newHTML = data;
$('#link1').html(data);
});
return false; // stop the link
});
$('#link2 a').click(function(){
var href=$(this).attr('href');
var querystring=href.slice(href.indexOf('?')+1);
$.post('rate.php', querystring, function(data){
var newHTML;
newHTML = data;
$('#link2').html(data);
});
return false; // stop the link
});
$('#link3 a').click(function(){
var href=$(this).attr('href');
var querystring=href.slice(href.indexOf('?')+1);
$.post('rate.php', querystring, function(data){
var newHTML;
newHTML = data;
$('#link3').html(data);
});
return false; // stop the link
});
As you can see, the code above is quite repetitive. The only difference amongst them is the link number selector (#link0, #link1, #link2, #link3, etc). How do I condense this code?
EDIT:
sorry i forgot to mention my html code is something like this:
<span id="link0">
Ratings:
1 <a href="http://blah/rate.php?id=1&ip=2&rating=u">THUMBSUP</a>,
0 <a href="http://blah/rate.php?id=1&ip=2&rating=d">THUMBSDOWN</a>
</span>
but you really ought to be using a class structure instead of tons of unique identifiers:
(if I was writing this)