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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:22:47+00:00 2026-06-02T16:22:47+00:00

I’m probably about a 7 or 8 on proficiency with jQuery (on a scale

  • 0

I’m probably about a 7 or 8 on proficiency with jQuery (on a scale of 1-10), so I’m not sure if this even makes sense, but I’d like to know if anyone knows of a jQuery function or possibly a plugin which allows a branch of jQuery to only be executed if a given condition is true. Otherwise, I’d love to hear if someone thinks the concept is flawed in some way (EDIT and how it is flawed)

While one could control attachment of various events using normal JavaScript syntax similar to this:

var desiredElement = $('.parent')                        // find the parent element
                     .hover(overFunction,offFunction)    // attach an event while I've got the parent in 'scope'
                     .find('.child-element');            // then find and return the child
if (booleanVar1) {                                       // if one condition
    desiredElement.click(clickFunction1);                //   attach one event
} else if (booleanVar2) {                                // or if a different condition
    desiredElement.click(clickFunction2);                //   attach a different event
} else {                                                 // otherwise
    desiredElement.click(clickFunction3);                //   attach a default event
}
$('.parent').find('.other-child')                        // (or $('.parent .other-child')
    .css(SomePredefinedCssMapping)
    .hide()
//...

I was wondering if there is a way to do it all in jQuery or if there is a good reason not to… something perhaps like this:

$('.parent')                                // find the parent element
    .hover({overFunction,offFunction})      // attach an event while I've got the parent in 'scope'
    .find('.child-element')                 // then find the child
        .when(booleanVar1)                  // if one condition
            .click(clickFunction1)          //   attach one event
        .orWhen(booleanVar2)                // or if a different condition
            .click(clickFunction2)          //   attach a different event
        .orElse()                           // otherwise
            .click(clickFunction3)          //   attach a default event
        .end()
    .end()
    .find('.other-child')
        .css(SomePredefinedCssMapping)
//...

Note: I think this is syntactically correct, assuming the booleans and functions are defined appropriately, but I’m pretty sure I’ve gotten the intent across pretty clearly

the proposed jQuery seems a little neater to me (??) agree/disagree? – so here are my questions:

  • Is there some part of native jQuery that basically already does this?
  • Is there an extension already out there that allows this type of thing?
  • Is it harder to do than I am thinking? (I’d think something like keeping the current element set if the condition is true, pushing an empty element set if condition is false, then popping the element set back out for each or condition would do it, just like the end() method pops back the previous set after a find() call)
  • Is there something that makes it significantly less efficient?

EDIT

The question asks how to do this with method chaining or why it would be unadvisable (specifics preferred). While it doesn’t ask for alternatives, such alternatives might be necessary to explain problems with a jQuery chaining approach. Also, since the example above immediately evaluates the booleans, any other solution should do the same.

  • 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-02T16:22:48+00:00Added an answer on June 2, 2026 at 4:22 pm
    $('.parent').hover(overFunction,offFunction)
        .find('.child-element')
        .click( booleanVar ? clickFunction1 :
                booleanVar2 ? clickFunction2 :
                clickFunction3 )
        .end()
        .find('.other-child')
        .css(SomePredefinedCssMapping)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't

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.