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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:02:59+00:00 2026-05-24T09:02:59+00:00

Since im currently still learning jQuery and am new to it i need a

  • 0

Since im currently still learning jQuery and am new to it i need a little bit of help cause im on a short deadline… What im trying to do is swap between two images when the image is clicked it should switch to the other and vice versa… Seems like it should be simple enough but for some reason its not working as intended… Heres my code:

UPDATED CODE

html/php

<div name='contractActions' style="float: right; width: 50px;">
    <input type="hidden" id="cId" value="<?= $contractId ?>" />
    <?
    if(mysql_result($result, $i,"IsHighlighted") == 0)
    {
        $imgIsHighlighted = _IMAGES . "highlighter_on.png";
        $altIsHighlighted = "Remove Highlight";
    }
    else
    {
        $imgIsHighlighted = _IMAGES . "highlighter_off.png";
        $altIsHighlighted = "Highlight";
    }
    ?>
    <span class="link-black"><a href="#" class="highlightAction">
        <img src="<?= $imgIsHighlighted ?>" border="0" alt="<?= $altIsHighlighted ?>" /></a>
    </span>
    <span class="link-black"><a href="#" class="declineAction">
        <img src="images/decline.png" border="0" alt="Decline" /></a>
    </span>
</div>

jQuery

$(".highlightAction").click(function() {
    var element = $(this).closest("div[name='contractActions']");
    var cId = $(element).find("#cId").val();
    var field = "IsHighlighted";

    $.ajax({
        type: "POST",
        url: "ajax/contract_buttons.php",
        dataType: "text",
        data: "contractId=" + cId + "&updateField=" + field,
        async: false,
        success: function(response) {
            $(element).find("a[class='highlightAction']").children("img").toggle(function() {
                $(this).attr("src", "images/highlighter_off.png");
                $(this).attr("alt", "Remove Highlight");
            },
            function() {
                $(this).attr("src", "images/highlighter_on.png");
                $(this).attr("alt", "Highlight");
            });
        }
    });
});

The code doesnt seem to work on the initial click afterwards however it toggles between them. Its as if the toggling doesnt activate until AFTER the first click and occasionaly after the second click. Any reasons as to why this might be happening? and a solution to this issue if possible thanks in advance!!

  • 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-24T09:02:59+00:00Added an answer on May 24, 2026 at 9:02 am

    Since I discussed it over chat the outcome was that well the code seems to be correct as it was working on the testing website: http://www.jsfiddle.net as stated by Jared Farrish. However for some reason it was giving “me” personally an issue, we couldnt find exactly why, but looked at another method of doing this as the .toggle() function seems to be slow… The other method would be through the use of classes, also instead of saving the highlight value in a DB it could be done using states which would also mean less hitting the server and DB speeding up processing time. Anyhow hope this helps if anyone comes around a similar issue…

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

Sidebar

Related Questions

We are currently using SharpZipLib but since it uses the GPL we need to
I am probably going to need to refactor in two steps since I'm still
Currently since I want to access user information in all my templates, I always
I'm currently experimenting with build script, and since I have an ASP.net Web Part
I'm currently implementing a raytracer. Since raytracing is extremely computation heavy and since I
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
I'm currently working with Groovy and Grails. While Groovy is pretty straight-forward since it's
I am currently rewriting a SDK to access a webservice. Since the model for
I am building a project involving natural language processing, since the nlp module currently
I am new to .aspx and now the thing is since i am doing

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.