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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:50:42+00:00 2026-05-21T21:50:42+00:00

Hey, I looked around on Stack Overflow and tried a few other people’s attempts

  • 0

Hey, I looked around on Stack Overflow and tried a few other people’s attempts at this, and I just couldn’t get this to work.

I have a select element like this:

<select id="slct_firstCat" name="slct_firstCat" size="15">
</select>

This only shows once a specfic AJAX request has completed, so this select element is NOT loaded with the original page’s DOM, it is loaded as a seperate AJAX request after the main page has loaded.

I basically want to do this…

if (/* select element exists */) {
  // Then change the colour of a div to green
} else {
  // Change the colour to red
}

But remember, this has to work after the page has loaded. I can click the select to run the JS function if needed.

I tried this code below, but couldn’t get it to work:

if ($(".element1").length > 0 || $(".element2").length > 0 {
  // code
}

So basically, if the select element exists, then change the tab colour to green, else change it to red.

  • 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-21T21:50:42+00:00Added an answer on May 21, 2026 at 9:50 pm

    You have to put your code in jQuery.ajax()‘s success callback. And the > 0 part isn’t needed in $('.element1').length > 0, since—unlike in Ruby—, 0 and "" are also falsy in JavaScript.

    jQuery.ajax({
        url: 'example.php',
        success: function () {    
            if ($('#slct_firstCat').length) {
                $('#myDiv').css('color', 'green');
            } else {
                $('#myDiv').css('color', 'red');
            }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, looked around but couldn't quite get an answer or figure it myself.
Hey guys, looked at the php docs and googled around, but no joy. This
Hey there stack overflow enthusiasts. I am getting this mixed content warning in Internet
Hey i have been looking around and i cant find a way to get
Hey all- I have looked this up on here and Google but none of
Hey everyone I've looked for hours trying to find a solution to this, my
Hey everyone I'm currently extremely frustrated and can not figure this out have looked
I've looked at the other threads on this topic, and none of them have
Hey guys i have looked as much as i could and i could not
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>

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.