Response title
This is preview!
<c:forEach var="post" items="${requestScope['postVotes']}">
<div class="thread" data-post="${post.key.messageId}"
data-vote="${post.value}"
data-response-level-message="${post.key.responseLevelMessage}">
<c:if test="${post.key.responseLevelMessage == 0}">
<hr>
<font size="2"> Le <fmt:formatDate
value="${post.key.messageDate}" /> par <a
href="${pageContext.request.contextPath}/CentralStation?section=votePage&CurrentContext=${post.key.messageId}">${post.key.messageSender}</a><br>
<br> ${post.key.messageTitle}<br>
${post.key.messageContent}<br>
<p class="postId"></p>
</font>
</c:if>
<c:if test="${post.key.responseLevelMessage > 0}">
<font size="1"> <a
href="${pageContext.request.contextPath}/CentralStation?section=votePage&CurrentContext=${post.key.messageId}">${post.key.messageSender}</a><br>
<br> ${post.key.messageContent}<br>
<p class="postId"></p>
</font>
</c:if>
<input type="image" class="like" src="img/up.png"
data-vote="${post.value}" value="Like">
<input
type="image" class="dislike" src="img/down.png"
data-vote="${post.value}" value="Dislike">
</div>
</c:forEach>
$(document).ready(function () {
$('.thread').each(function () {
//Previons function
var thread = $(this);
var val = thread.data("vote");
thread.find("p").text(val);
if (val == 1) {
thread.find(".like").addClass('voted');
}
else if (val == -1) {
thread.find(".dislike").addClass('voted');
}
});
});
$(document).ready(function () {
$('.thread').each(function () {
var thread = $(this);
//Treat like button
thread.on('click', this.children("input.like"), function () {
//Retrive vode and increment
var vote = thread.data("vote");
if(vote <= 0){
vote = 1;
}
else if (vote == 1) {
vote = 0;
}
//modify data-vote
thread.data("vote", vote);
//print vote
thread.find("p").text(vote);
if (vote == 1) {
thread.find(".like").addClass('voted');
thread.find(".dislike").removeClass('voted');
} else if (vote == 0){
thread.find(".dislike").removeClass('voted');
thread.find(".like").removeClass('voted');
}
});
//Treat dislike button
thread.on('click', this.children("input.dislike"), function () {
//Retrive vode and increment
var vote = thread.data("vote");
if(vote >= 0){
vote = -1;
}
else if (vote == -1) {
vote = 0;
}
//modify data-vote
thread.data("vote", vote);
//print vote
thread.find("p").text(vote);
if (vote == -1) {
thread.find(".like").removeClass('voted');
thread.find(".dislike").addClass('voted');
} else if (vote == 0){
thread.find(".dislike").removeClass('voted');
thread.find(".like").removeClass('voted');
}
});
});
});
//Treat like button
thread.on('click', this.children("input.like"), function () {...
//Treat dislike button
thread.on('click', this.children("input.dislike"), function () {...
//Treat like button
thread.on('click', 'input[value="Like"]', function () {...
//Treat dislike button
thread.on('click', 'input[value="Dislike"]', function () {...
SO, can someone tell me why jQuery behaves this way and why the first way of writing my functions is not the good one ?
Thanx for your help.
© 2013 jQuery Foundation
Sponsored by and others.