If you are looking for some strategy to focus events on the repeating piece of data, using identifiers with the number added for the link may work, but it may not be the best.
, , . , .
, :
<div id='outerContainer'>
<div id='set_123' class='someContainer'>
<a href='#' class='vote_up'><img src="img/uparrow.png" /></a>
<span class='vote_count'></span>
<a href='#' class='vote_down'><img src="img/downarrow.png" /></a>
</div>
<div id='set_124' class='someContainer'>
<a href='#' class='vote_up'><img src="img/uparrow.png" /></a>
<span class='vote_count'></span>
<a href='#' class='vote_down'><img src="img/downarrow.png" /></a>
</div>
<div id='set_125' class='someContainer'>
<a href='#' class='vote_up'><img src="img/uparrow.png" /></a>
<span class='vote_count'></span>
<a href='#' class='vote_down'><img src="img/downarrow.png" /></a>
</div>
</div>
.delegate() #outerContainer, /.
- :
$(document).ready(function() {
$('#outerContainer').delegate('.vote_up', 'click', function() {
var the_id = $(this).closest('.someContainer').attr('id').split('_').pop();
$.ajax({
type: "POST",
context: this,
data: "action=vote_up&id=" + the_id,
url: "ajax/votes.php",
success: function (msg) {
$(this).siblings("span.vote_count").html(msg).fadeIn();
$(this).children("img").attr("src", "img/uparrowActive.png");
}
});
});
$('#outerContainer').delegate('.vote_down', 'click', function() {
var the_id = $(this).closest('.someContainer').attr('id').split('_').pop();
$.ajax({
type: "POST",
context: this,
data: "action=vote_down&id=" + the_id,
url: "ajax/votes.php",
success: function (msg) {
$(this).siblings("span.vote_count").html(msg).fadeIn();
$(this).children("img").attr("src", "img/downarrowActive.png");
}
});
});
});
, .someContainer. , , .split().pop().
AJAX context: $.ajax(), this .
.siblings(), .vote_count, .html().
, .children(), img, src.
. HTML.