CLiown
CLiown

Reputation: 13843

Evaluate in Jquery

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

Answers (5)

James Wheare
James Wheare

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 the tr elements that contain a table.navitem immediately after a tr that contains a table.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 be linksbelow, otherwise Nolinksbelow.

Upvotes: 0

Ferdinand Beyer
Ferdinand Beyer

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

VoteyDisciple
VoteyDisciple

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 .navheaders 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

annakata
annakata

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

maxpower47
maxpower47

Reputation: 1656

Not really sure what you're asking, you just need the syntax for if/else in js? Try this

Upvotes: 0

Related Questions