Question

I have really long id's that are rendered so in some cases, when manipulating those with jQuery, it makes sense to create variables.

So for example if I have:

    if (!$(e.target).is('#some--wickedly-absurd-long-form-id-name-on-this-page, 
#some--wickedly-absurd-long-form-id-name-on-this-page *')) { ...

In the code above, I name the element and then the element and anything under it with a wildcard.

That's not really ideal. So I create a variable as:

var showhideWrapper = $("#some--wickedly-absurd-long-form-id-name-on-this-page");

I then try to do the same thing I did above with the id names but it does not work.

    if (!$(e.target).is(showhideWrapper, showhideWrapper *)) { ...

I tried various combinations

    if (!$(e.target).is(showhideWrapper, showhideWrapper + '*')) { ...

... but I don't really know the nuance here to get it to work.

Was it helpful?

Solution

You could do something like this:

if (!showhideWrapper.find(e.target).length) {

(Actually, that isn't perfectly equivalent, since it doesn't test the showhideWrapper itself. The point though, is that you need to use traversal functions. You can't concatenate objects with strings and get a useful selector).

But I would probably go the other way, and this is generally the standard way to see whether an event occurs within a certain element:

if(!$(e.target).closest(showhideWrapper).length) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top