Reputation: 13843
Just wondering if its possible to convert the following to an IF statement in Javascript:
$('.Nav table tr:has(table.navheader) + tr:has(table.navitem)').addClass('linksbelow');
Example:
IF $('.Nav table tr:has(table.navheader) + tr:has(table.navitem)').addClass('linksbelow'); = **TRUE** (
$('.Nav table .navheader').addClass('linksbelow');
)
**ELSE** (
$('.Nav table .navheader').addClass('Nolinksbelow');
Upvotes: 0
Views: 280
Reputation: 4800
I'm not following how your first line of code relates to the "Example". Could you describe the effect you're trying to achieve in words?
Your first line reads:
(1) Add the class
linksbelow
to all thetr
elements that contain atable.navitem
immediately after atr
that contains atable.navheader
within.Nav table
And the example reads:
(2) Add a class to any element that matches
.Nav table .navheader
. The classname is dependent on the result of (1). If the operation in (1) succeeded (presumably meaning if it matched any elements) then the classname should belinksbelow
, otherwiseNolinksbelow
.
Upvotes: 0
Reputation: 67177
Here is my guess of what you could possibly want:
var q = $('.Nav table tr:has(table.navheader) + tr:has(table.navitem)');
if (q.length) {
q.addClass('linksbelow');
$('.Nav table .navheader').addClass('linksbelow');
} else {
$('.Nav table .navheader').addClass('Nolinksbelow');
}
Upvotes: 1
Reputation: 37813
You could just approach it this way:
$('.Nav table .navheader').addClass('Nolinksbelow');
$('.Nav table tr:has(table.navheader) + tr:has(table.navitem)').addClass('linksbelow').find('.navheader').addClass('linksbelow').removeClass('Nolinksbelow');
Here I start by assuming that no .navheader
s have links, then go apply the linksbelow
class as you did before, and then (here's the important bit) find() the appropriate .navheader
children and correct those by removing the (now incorrect) Nolinksbelow
class and adding the correct linksbelow
class.
Upvotes: 3
Reputation: 75862
You are aware that jquery is javascript aren't you?
Implicitly the answer to your question is therefore "yes", but you end up with a bit of javascript which feels suspiciously similar to a small part of jquery...
Upvotes: 0
Reputation: 1656
Not really sure what you're asking, you just need the syntax for if/else in js? Try this
Upvotes: 0