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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:42:37+00:00 2026-06-10T08:42:37+00:00

I thought this would be simple: all I want is for JS to detect

  • 0

I thought this would be simple: all I want is for JS to detect if the child img of the clicked link has a certain src attribute. If yes, do one thing. If no, do this other thing. But for some reason the click event is ignoring the if/else condition entirely. I can put any nonsense I want if the .children() selector and the event will still execute as if the condition is true. I don’t know Jquery very well, so I’m at a loss. Any help would be appreciated.

$('.addcomp').click(function(){
                if($(this).children('img[src="add.png"]')){
                    $(this).parents("tr").find('.compcost').addClass('cost').end().find('td:last-child,td:nth-child(3)').addClass('addcompbg');
                    $(this).children("img").attr("src","forbidden.png");
                }
                else if ($(this).children('img[src="forbidden.png"]'))
                {$(this).parents("tr").find('.compcost').removeClass('cost').end().find('td:last-child,td:nth-child(3)').removeClass('addcompbg');
                    $(this).children("img").attr("src","add.png");
                    }
            });
  • 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-06-10T08:42:39+00:00Added an answer on June 10, 2026 at 8:42 am

    Use $(this).children('img[src="add.png"]').length instead. $() will always return a jQuery object, whether there is a match or not, hence it’ll always be evaluated as true in the if.

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

Sidebar

Related Questions

I thought this would be simple, but I'm struggling. I want to manage Subversion
I thought this would be fairly simple but it turns out not to work
I would have thought this one would be simple but I'm trying to change
OK so I thought this would be pretty simple but it's tripping me up..
I thought it would be as simple as this... $('#yourSelect').change(function () { if ($(this).val()
This is what I thought would be a simple select clause, however the following
This should be so simple... I thought it would be nice to try the
I thought this would be fairly easy, but I'm totally baffled. I want one
I thought this would be very easy. I downloaded the JDK, extracted src.zip, modified
I thought this would be pretty easy but I'm running into all sorts of

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.