views:

94

answers:

5

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');
A: 

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

maxpower47
A: 

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

annakata
Yes I am aware of this, however unaware of how to achive.
danit
I guess my question is what's the gain? Presumably you aren't allowed to use jquery?
annakata
No I can use jquery, I just dont know how? If you can shed some light i've appreciate it. Either answer above doesnt result in the desired effect.
danit
+3  A: 

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.

VoteyDisciple
+1  A: 

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');
}
Ferdinand Beyer
A: 

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.

James Wheare