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

  • Home
  • SEARCH
  • 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 3230358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:55:03+00:00 2026-05-17T16:55:03+00:00

i have this jQuery file, but the vote_up click handler keeps conflicting with the

  • 0

i have this jQuery file, but the vote_up click handler keeps conflicting with the vote_down click handler, when i click the vote_down element it changes the vote_up element:

jQuery script:

$(document).ready(function () {
    $("a.vote_up").click(function () {
        //get the id
        var the_id = this.id.split('_').pop();
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_up&id=" + the_id,
            url: "ajax/votes.php",
            success: function (msg) {
                $("span.vote_count#" + the_id).html(msg).fadeIn();
                $("#" + the_id + " img").attr("src", "img/uparrowActive.png");
            }
        });
    });
});
$(document).ready(function () {
    // the vote down function
    $("a.vote_down").click(function () {
        //get the id
        var vote_id = this.id.split('_').pop();
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_down&id=" + vote_id,
            url: "ajax/votes.php",
            success: function (msg) {
                $("span.vote_count#" + vote_id).html(msg).fadeIn();
                $("#" + vote_id + " img").attr("src", "img/downarrowActive.png");
            }
        });
    });
});

html:

<a href='#' class='vote_up' id="id_23"><img src="img/uparrow.png" /></a>
<a href='#' class='vote_down' id="id_23"><img src="img/downarrow.png" /></a>

the jQuery and ajax request is wokring fine, but the change of src is the problem, because when i click vote down, it changes the vote_up image!!

  • 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-17T16:55:04+00:00Added an answer on May 17, 2026 at 4:55 pm

    If you’re looking for some sort of strategy for focusing events to a repeating piece of data, utilizing IDs with the number appended to reference the various elements may work, but may not be the best approach.

    I assume each repeating item has its own container that repeats. You may be better off placing a unique ID on that container, and finding the elements from there.

    Take this for example:

    <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>
    

    You could use .delegate() to place click handlers on the #outerContainer that fire when you click the appropriate up/down elements.

    Something like:

    $(document).ready(function() {
        $('#outerContainer').delegate('.vote_up', 'click', function() {
           //get the id
            var the_id = $(this).closest('.someContainer').attr('id').split('_').pop();
            //the main ajax request
            $.ajax({
                type: "POST",
                  // Make sure "this" in the callback refers to the element clicked
                context: this,
                data: "action=vote_up&id=" + the_id,
                url: "ajax/votes.php",
                success: function (msg) {
                      // Not sure where your vote_count is. See the HTML for my placement
                    $(this).siblings("span.vote_count").html(msg).fadeIn();
                      // get the child <img> and set its src
                    $(this).children("img").attr("src", "img/uparrowActive.png");
                }
            });
        });
        $('#outerContainer').delegate('.vote_down', 'click', function() {
           //get the id
            var the_id = $(this).closest('.someContainer').attr('id').split('_').pop();
            //the main ajax request
            $.ajax({
                type: "POST",
                  // Make sure "this" in the callback refers to the element clicked
                context: this,
                data: "action=vote_down&id=" + the_id,
                url: "ajax/votes.php",
                success: function (msg) {
                      // Not sure where your vote_count is. See the HTML for my placement
                    $(this).siblings("span.vote_count").html(msg).fadeIn();
                      // get the child <img> and set its src
                    $(this).children("img").attr("src", "img/downarrowActive.png");
                }
            });
        });
    });
    

    So the ID with the number you need is on each .someContainer. You traverse up to that container to get the ID, and do your .split().pop().

    Then in the AJAX request, I set the context: property for $.ajax() so that this will still refer to the element that was clicked in your callback.

    Inside the callback, you find the .siblings() that have the class .vote_count, and set its .html() content.

    Finally you use .children() to get the img element, and set its src attribute.

    This should give the general idea. You’ll need to adjust for your HTML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jquery script to call an external file. So far so good.
I have this jQuery which works fine $(li[id^='shop_id']).click( function () { alert(I clicked on
I have this code: var $msg = jQuery('<div></div>') .hide() .appendTo(document.body) ; if ($msg.is(:hidden)) {
This probably sounds really stupid but I have noo idea how to implement jquery's
I have this jquery function function example(file, targetwidget, callback){ $(targetwidget).load(file, {limit: 25}, function(){ $(#widget_accordion).accordion({fillSpace:
I have an Ajax contact form that links to a jquery file but for
I have this code, which gets an clicked div id: $(document).ready(function(){ $(.playitem).click(function(){ pos =
I have this jquery script: $('[id^=changesetList] tr').each(function () { var sid = $(this).attr('sid'); $(this).find('td
OK, i have this jQuery script using ajax to load a response from a
I have this ajax jquery code: var form = document.getElementById('frm'); var data_string = form.serialize();

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.