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 6992193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:36:09+00:00 2026-05-27T19:36:09+00:00

jQuery: $(.upvote).click(function() { var id = $(this).attr(‘id’); var changeImage = ($(this).children(‘img’).attr(‘src’) === siteUrl +

  • 0

jQuery:

$(".upvote").click(function() {
    var id = $(this).attr('id');

    var changeImage = ($(this).children('img').attr('src') === siteUrl + '/images/icon-upvote.png')
                    ? siteUrl + '/images/icon-upvote-inactive.png' 
                    : siteUrl + '/images/icon-upvote.png';
    $(this).children('img').attr('src', changeImage);

    var changeClass = ($(this).children('img').attr('src') === siteUrl + '/images/icon-upvote.png')
                    ? 'vote-icon' 
                    : 'vote-icon semi-transparent-more';
    $(this).children('img').attr('class', changeClass);

    $.ajax({
        type: 'GET',
        data: 'comic=' + id + '&type=upvote',
        url: siteUrl + '/vote.php',
        success: function() {
            // PHP does all checks
        }
    });
});

HTML:

<div>
    <a href="vote.php?comic=$fileName&amp;type=upvote" id="$fileName" class="upvote$guestLink" onclick="return false;">$upvoteImage</a>
</div>
<div style="margin: 5px 0 8px 0;">
    <span class="score$scoreStyle bold" id="$fileName">$fileScore</span>
</div>
<div>
    <a href="vote.php?comic=$fileName&amp;type=downvote" class="downvote$guestLink">$downvoteImage</a>
</div>

This is what I’ve got and it works really well. Problem is I know there’s a way to make this a tad more efficient than what I have.

Also, how can I select that span to change it’s value? And how would I go about adjusting the value based on what is currently set within it? I’ve been struggling on this for the longest time.

  • 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-27T19:36:10+00:00Added an answer on May 27, 2026 at 7:36 pm
    $(".upvote").click(function() {
        var id = this.id,
            $img = $(this).children('img'),
            changeImage,
            changeClass;
    
        if ($img.attr('src') === siteUrl + '/images/icon-upvote.png') {
            changeImage = siteUrl + '/images/icon-upvote-inactive.png';
            changeClass = 'vote-icon';
        } else {
            changeImage = siteUrl + '/images/icon-upvote.png';
            changeClass = 'vote-icon semi-transparent-more';
        }
    
        $img.attr({'src' : changeImage,
                   'class' : changeClass });
    
        $.ajax({
            type: 'GET',
            data: 'comic=' + id + '&type=upvote',
            url: siteUrl + '/vote.php',
            success: function() {
                // PHP does all checks
            }
        });
    });
    

    What I’ve changed:

    1. Setting the id variable: given that in the function this is the DOM element, you can say this.id directly instead of creating a jQuery object with $(this).attr('id')
    2. Cache your main jQuery object: rather than repeating $(this).children('img') (I’ve created a variable $img for this.)
    3. Added an if/else: the ternary operator ? is nice in a general sense, but you were using it twice (once for changeImage and once for changeClass) to evaluate the same condition. To me it makes more sense to evaluate that condition once in an if and then set both variables.
    4. Changing the attributes: you were calling .attr() twice, but you can pass it a map of attributes and change them all at once.

    Also, how can I select that span to change it’s value? And how would I go about adjusting the value based on what is currently set within it?

    Well it would be a little easier to select if it was a child of the same div as the up and down anchor elements, but anyway for your structure maybe something like this:

    // within the click handler for the upvote:
    var $score = $(this).parent().next().find("span");
    // or within the handler for the downvote:
    var $score = $(this).parent().prev().find("span");
    

    Note also that you’ve given that span an id that is the same as the id of the anchor element above it – this makes your html invalid because id should be unique on the page. I assume that you can’t select by id though because you have multiple voting elements on the page.

    As far as setting the value, to get the current value and increment in JS:

    $score.html(function(i, oldHTML) {
       return +oldHTML + 1;
    });
    

    The syntax of passing a callback to .html() gives you a parameter with the old value and then you return the new value to set. So +oldHTML converts the string to a number – obviously I’m assuming here the span contains nothing but a number – and then adds one to it.

    However, I think it would be better to do that in your ajax success handler using data from the server: have your PHP code return the new value and then in the success handler apply that value.

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

Sidebar

Related Questions

I have this: $(#upvote).click(function(){ var up = parseInt(document.getElementById('voteScore').innerHTML); up++; document.getElementById('voteScore').innerHTML = up; $.ajax({ type:
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
jQuery.widget(ui.test,{ _init: function(){ $(this.element).click(this.showPoint); }, showPoint: function(E){ E.stopPropagation(); alert(this.options.dir); } } $('#someEleme').test(); Right now,
I have this function: $(function() { $('.upvote').click(function() { voteActions(upvoteID, this.id); } ); $('.downvote').click(function() {
jQuery(document).ready(function(){ $(function() { $('.button-a').each(function() { $(this).hover( function () { $('.button-title', this).animate({ opacity: 0.6 },
I think I got the jQuery imports sorted like this: <script type=text/javascript src=https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js></script> <script
Jquery Code: $(.tog).toggle( function() { $(.tog span).removeClass('stog'); $(.tog span).addClass('stogm'); $(this).next().slideDown('fast'); }, function() { $(.tog
I am trying to select .singleAnswer but it's not working. JQUERY: $(.answerContainer).on(click, .editAnswer, function(e){
I know this is silly, but there's any difference between this: (function() { var
jQuery provides a browser neutral library for accessing and manipulating the DOM. This is

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.