views:

39

answers:

4

Looking at this jQuery example, how can I modify the code so that it only changes the color of the cell if the value of the submit button in that cell is a certain value.

i.e.-

          var submitEl = $("td :submit")

          //Only do the below if the submit buttons value is "XYZ"

          .parent('td')
          .css({background:"yellow", border:"3px red solid"})
+3  A: 
$("td input[value='SomeValue']:submit")
Darin Dimitrov
+1  A: 
var submitEl = $('td :submit').filter(function() { return $(this).val() == "certain"; });

You can check for the value in the selector, but it may lead to quoting headaches (depending on the value) and also it may not be quite as fast (though that's rarely a serious concern).

Pointy
+1  A: 

select only the elements that have the value you want:

var submitEl = $("td :submit[value='XYZ']") 
dnagirl
I don't think you want that space between `:submit` and the left bracket.
Pointy
@Pointy: good eye. fixed.
dnagirl
+1  A: 

you need a each loop I think this is what your trying to do

  $("td :submit").each(function(){
   if ($(this).val()== "XYZ"){
     $(this).parent('td').css({background:"yellow", border:"3px red solid"});
   }
   });

EDIT

using better selector eliminates if statment

  $("td:submit[value='XYZ']").each(function(){
     $(this).parent('td').css({background:"yellow", border:"3px red solid"});
  });"
mcgrailm
You don't really need an "each" loop if you filter with something in the selector itself or with a separate "filter()" in the chain.
Pointy
so are you saying it could be done like this $("td:submit[value='XYZ']").parent('td').css({background:"yellow", border:"3px red solid"});
mcgrailm
Yes. Now, thinking about this a little, one could argue that doing the filtering and the operation in a single function (as per your original) might actually be a little more efficient. The "filter" is useful of course if you want to hold on to the list, in case something needs to be done to the same elements later.
Pointy
hmm interesting always seems to be eight ways to do any one thing 8 ^ )
mcgrailm