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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:32:42+00:00 2026-05-19T04:32:42+00:00

I’m a jQuery newbie and am trying to port a script It displays a

  • 0

I’m a jQuery newbie and am trying to port a script

It displays a pie chart image (initial class: “primary”) and a bar chart image (initial class: “secondary”, not visible) for the same data and viewers can click the chart to flip between pie and bar:

<style>
        img.primary   {
                display: inline;
                cursor: pointer;
        }
        img.secondary {
                display: none;
        }
        p.chart:hover, img.secondary {
                display: inline;
                cursor: pointer;
        }
        p.chart:hover, img.primary {
                display: none;
        }
</style>

<script type="text/javascript" src="jquery-144.js">
</script>

<script type="text/javascript">

$(function() {
        // remove the "chart" class when the page loads,
        // to disable hover for JavaScript-enabled users
        $("p.chart").removeClass("chart");

        // XXX add a click-handler for P.chart here?
});

// XXX how to rewrite this in jQuery?
function swapImages(container) {
        for (var child in container.childNodes) {
                child = container.childNodes[child];
                if(child.nodeName == "IMG")
                        child.className = (child.className == "primary" ? "secondary" : "primary");
        }
}
</script>

And here is an excerpt from the web page:

<p class="chart" onclick="swapImages(this);">
<img class="primary" width=700 height=280 src="pie.png">
<img class="secondary" width=700 height=280 src="bar.png">
</p>

(Note: the “p.chart” class is removed on load so that the charts flip between pie and bar on mouse-hover only for JavaScript disabled users)

But my actual question is about how to rewrite the swapImages() function from pure JavaScript to jQuery. I probably should call:

$('p.chart').click(function(e) {
      ....
}

and call addClass(“primary”) or removeClass(“secondary”) there, but I’m missing the bit on how to go through all IMG-children of that P.chart.

And do I need to call $(‘p.chart’).click(….) or $(‘p.chart’).each().click(….) ?

Or maybe I don’t need p.chart here anymore and just assign click-handlers directly to the images?

Thank you! Alex

  • 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-19T04:32:43+00:00Added an answer on May 19, 2026 at 4:32 am

    You can do this:

        $(function(){
          $('p.chart').removeClass(chart);
          $('p:has(img)').click(function(e){
            $("img", this).toggleClass("primary").toggleClass("secondary");
        });
       });
    

    If there are two images and you need to toggle the images on each click you can simply use the toggle function:

     $(function(){
          $('p.chart').removeClass(chart);
          $('p:has(img)').click(function(e){
             $("img", this).toggle();
            });
       });
    

    Edit: Updated the post as per the comment by OP.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.