Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3434456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:42:02+00:00 2026-05-18T07:42:02+00:00

I thought this could be achieved with the .prev() function but for some reason

  • 0

I thought this could be achieved with the .prev() function but for some reason it isn’t working.

I’m creating thumbs up/down buttons for posts on a blog. I’m trying to display messages according to what the user votes. Either UP or DOWN but whenever I vote 1 particular post, the message appears for all the post.

This is my code. I removed the prev() attempts to make it more readable. The script works fine ajax wise.

$(document).ready(function() {
    $(".vote_button").click(function(e) { //the UP or DOWN vote button
    var vote_status = $(this).attr('class').split(' ')[1]; //gets second class name following vote_button
    var vote_post_id = $(this).attr("id"); //the post ID
    var dataString = 'post_id=' + vote_post_id + '&vote_status=' + vote_status;

    $.ajax({
        type: "POST",
        url: "url/add_vote.php",
        data: dataString,
        cache: false,
        success: function(html) {
            if (vote_status == 1) 
         {
                $('.msg_box').fadeIn(200);
                $('.msg_box').text('You voted UP!');
            }
            if (vote_status == 2) 
         {
                $('.msg_box').fadeIn(200);
                $('.msg_box').text('You voted DOWN!');
            }
        }
    });
    return false;
});
});

Sample HTML

<div class="vote_button 1" id="18">UP</div>
<div class="vote_button 2" id="77">DOWN</div>
<div class="msg_box"></div>

<div class="vote_button 1" id="43">UP</div>
<div class="vote_button 2" id="15">DOWN</div>
<div class="msg_box"></div>


<div class="vote_button 1" id="11">UP</div>
<div class="vote_button 2" id="78">DOWN</div>
<div class="msg_box"></div>

EDIT: Provided jsfiddle without Ajax part
http://jsfiddle.net/XJeXw/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:42:03+00:00Added an answer on May 18, 2026 at 7:42 am

    You need to save a reference to the button inside the click handler (eg, var me = $(this);), then use me.nextAll('.msg_box:first') inside the AJAX handler.

    EDIT: Example:

    var me = $(this);   //The this will be different inside the AJAX callback
    
    $.ajax({
        type: "POST",
        url: "url/add_vote.php",
        data: dataString,
        cache: false,
        success: function(html) {
            me.nextAll('.msg_box:first')
                .text(vote_status == 1 ? 'You voted UP!' : 'You voted DOWN!')
                .fadeIn(200);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is perhaps more of a discussion question, but I thought stackoverflow could be
Perhaps a fresh set of eyes could help me with this. I thought this
I'm using the Dapper micro ORM. I thought it could handle enumerables like this:
I thought this question would have already existed on SO, but then I couldn't
I thought this would be fairly simple but it turns out not to work
I thought this would be fairly easy, but I'm totally baffled. I want one
I thought this was covered elsewhere but I don't see it now. Anyway, having
I thought this would be pretty easy but I'm running into all sorts of
I thought this was a privilege issue but I logged on as admin and
I thought it was clear, but doesn't seem so. This question is about T-SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.