views:

60

answers:

3

how do i get the id under another id in an if statement?

$('#mainmenu').mouseenter(function () {
  if ( $(this).???('#a')) {
        }  

  if ( $(this).???('#b')) {
        }  
});

<div id="mainmenu">
    <div id="a"></div>
    <div id="b"></div>
</div>
+3  A: 

You want the find method.

$(this).find('#a')

Fyodor Soikin
+1  A: 

Try this:

$('#mainmenu').mouseenter(function () {

  if ($('#a', $(this)) {
    // your code here...............
  }  

  if ($('#b', $(this)) {
    // your code here...............
  }  

});
Sarfraz
Technically the answer, but a weird way to get there.
R0MANARMY
I maybe don't understand something, but... How exactly is it supposed to work?
Fyodor Soikin
Ah! I see. Did you mean "$( '#a', $(this) )" ?
Fyodor Soikin
@Fyodor: I hope so, otherwise it's entirely pointless :P
Matt
@Sarfraz: No, it doesn't. Look closer, you'll see. (I'm ruling out the possibility that you simply may not know Javascript...)
Fyodor Soikin
@Fyodor Soikin: you picked up good point, i did not notice that, Thanks
Sarfraz
@Sarfraz: You're code will find the element with ID b, then check whether it's within $(this). Whilst my previous comment was probably a little harsh for the scenario in question, imagine the situation `$($('.someClass'), $(this));` where someone is attempting to speed up the selector. I'd prefer to use `$(raw-selector, filter)` throughout.
Matt
@Matt: agreed but i changed that even before your comment, thanks anyways.
Sarfraz
+1  A: 
$('#mainmenu #a, #mainmenu #b').mouseenter(function(){
 // code for something cool...
});

HTH.

Sunny
Would be easier to do `$("#a, #b", $("#mainmenu")).mouseenter(function(){ // ... });`
R0MANARMY
@RomanArmy, I prefer the CSS selector convention, each to his own poison I guess
Sunny
This changes the intended behavior.What if "#mainmenu" has something else inside, besides #a and #b?
Fyodor Soikin
From the example the OP has given I am under the impression that only 2 elements with defined ID's are to be selected...
Sunny
thanks for all your suggestions, i'm sure they all work but i got my animatiions working with Sunny's code + if ( $(this).is('#a')) {Thank you for your time.