views:

222

answers:

4

I'm having difficulty understanding why a particular selector isn't working for me. Admittedly I'm a bit of a JQuery newbie.

This correctly selects the first div.editbox on the page and colors it yellow:

$('div.editbox:first').css("background-color","yellow");

However, this if ... is construct makes the highlighted border appear for each box as it is moused-over.

$('div.editbox').bind('mouseover', function(e) {
    if ($(this).is('div.editbox:first')) {$(this).css("border", "1px solid red");}
});

I have tried variations such as '.editbox :first', '.editbox div:first', etc.

Essentially, I want to be able to reliably test whether this is the first or last element of with the class name.

Thanks!

Edit: here's the HTML I'm using:

<body>
<div class="container">
<p></p>
<div class="editbox" id="box1">Foo</div>
<div class="editbox" id="box2">Bar</div>
<div class="editbox" id="box3">Baz</div>
<div class="responsebox" id="rbox"></div>
</div>
</body>

This is just a proof-of-concept page; the actual page will of course be much more complex. Again: what I want is to reliably detect if I am in the first or last "div.editbox". A workaround I used is:

$('div.editbox:last').addClass("lasteditbox");

Then test for if ($(this).is(".lasteditbox")) which works, but it seems clumsy and I'm trying to learn the proper way to do this with JQuery.

+2  A: 

If you want the mouseover on just the first occurence of the class editbox inside div

$('div.editbox:first').mouseover(function() {
   $(this).css("border", "1px solid red");
});

Edit

$('div.editbox').mouseover(function() {
   $(this).css("border", "1px solid yellow");
}).filter(':first').mouseover(function(){
  $(this).css("border", "1px solid red");
}).filter(':last').mouseover(function(){
  $(this).css("border", "1px solid blue");
})
TStamper
Thanks TStamper; the CSS is only as an example. I have a function running on all the elements, but I want it to behave differently on the first and last elements, so I need to be able to detect when the function is running inside those.
SylvanK
updated to compare for all
TStamper
I'm very sorry TStamper; I appreciate the approach you're taking, but I'm attaching a moderately complex event handler to each element. Your approach would mean I need to have 3 separate versions of that function; or alternately, take an argument to the function when bound as to whether this is the first, last, or a middle element. I suppose that approach could work, but again, I'm trying to find the "right" JQuery way of doing this. Is what I'm doing with $(this).is(".class:first") really not workable?
SylvanK
Wont the second filter only apply to the subset of the first filter? Therefore the blue mouse over doesn't have any effect? (I could be wrong, I'm new to JQuery)
Pool
@Nick- the second filter is not nested within the first filter, so that selector is not applied to the next one
TStamper
A: 

have u tried

if ($(this).is(':first')) {$(this).css("border", "1px solid red");}
ssmithstone
That catches every box again.
SylvanK
+2  A: 

UPDATE: This works for the first element.

$('div.editbox').bind('mouseover', function(e) {
 if ($("div.editBox").index(this) == 0) {
      $(this).css("border", "1px solid red");
    }
});

And for the last element, this selector works:

if($("div.editBox").index(this) == ($("div.editBox").length-1)){
     $(this).css("color","red");
}
Jose Basilio
Thanks, but that only picks it if it's the first child of the parent container; not the first element with the class name.
SylvanK
Please try my update
Jose Basilio
Sorry, doesn't pick it up at all. I'll add the html I'm using above.
SylvanK
I finally got it to work. Try this.
Jose Basilio
Thank-you José, that does indeed work! This is definitely usable, and more elegant than my kludged assignment of extra classes to the first and last elements. However, part of this exercise for me was to understand why my use if `is` wasn't working the way I was trying it. I'm still not sure why the .editbox:first selector works fine for assigning a style, but the "is" comparison doesn't work the same way.
SylvanK
It's probably a bug in jQuery
Jose Basilio
good thought, I couldn't think of an alternative way
TStamper
A: 
    $('div.editbox').mouseover(function() {
       //change the css for all the elements
       $(this).css("background-color", "yellow");
    })
    .filter(':first,:last').mouseover(function(){
      //execlude the first and the last
      $(this).css("background-color", "Blue");
    })
Marwan Aouida