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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:42:08+00:00 2026-05-26T10:42:08+00:00

What I have now: var result = $(‘selector1’); if (result.length == 0) result =

  • 0

What I have now:

var result = $('selector1');
if (result.length == 0) result = $('selector2');

but this defeats chaining.

Question is – how do I get same result with JQuery chaining?

I can’t use $('selector1, selector2'), because this would always select result sets for both selectors, while I need results of selector2 only if there are no matching elements for selector1.

  • 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-26T10:42:09+00:00Added an answer on May 26, 2026 at 10:42 am

    This behavior in some places is called “coalescing”. Here’s a generic jQuery plugin that does it for you (editing after great feedback, see the comments).

    // The namespace function
    jQuery.coalesce = function(selectors){
    
        var out;
    
        $.each(selectors, function(i, v){
            var el = jQuery(v);
            if (el.length) {
               out = el;
               return false;
            }
        });
    
        return out || jQuery();
    };
    
    // The jQuery plugin
    jQuery.fn.coalesce = function(){
        return jQuery.coalesce(this.selector.split(",")); //a little brittle
    };
    

    So, in a world where #foo doesn’t exist, and a and div do, if you do:

    jQuery.coalesce(["#foo", "a", "div"])
    

    That returns jQuery("a") if #foo doesn’t exist, or jQuery("#foo") if #foo does exist.

    If you require using it in the middle of the chain, you can use $("#foo, a, div").coalesce(), but its vulnerable to commans within the selectors themselves.

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

Sidebar

Related Questions

Right now I have this jQuery('.widget-prop').keyup(function() { var prop = jQuery(this).attr('id'); var val =
i have the following query: $timecheck = $db->query(SELECT (B <= NOW()) AS var FROM
I have opened a new window with JavaScript: var newwin = window.open('','preview','width=600,height=500'); Now I
I have one array in javascript like var arr = [aa,bb,cc, dd]; and now
I have now been stuck at this for some time so I though to
What I have now is this page . And I want the section title
I have following query: var result = ( from role in db.Roles join user
i have this pattern i using to replace string: var html = some test
Consider this scenario. I have some business logic that now and then will be
I have defined the following variable var result = from report in firoozehDataContext.Reports select

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.