1

I have callout label associated with multiple controls, which should turn red when either of it is wrong.

<tr>
    <th>
        <asp:Label for="test">jquery will make my class as updnValidationErrorLabel</asp:Label>
    </th>
    <td > this text is right
        <asp:TextBox class='textboxWide taskListCheckPreVital' />
    </td>
    <td>this is wrong text hence it has updnValidationErrorInput
        <asp:TextBox class='dummyclass updnValidationErrorInput'/></td>

</tr>
​

I'm trying this approach, but not sure why mainparent children element does not show with class updnValidationErrorInput

//if my sturcture has updnValidationErrorInput
$('.updnValidationErrorInput').each(function() {
  // go to tr element
  var mainParent = $(this).parents('tr:first');
  // under tr element find updnValidationErrorInput
  if(mainParent.children('.updnValidationErrorInput').length > 0){
  // set label which has for attribute with    updnValidationErrorLabel
  mainParent.children('label').attr('for').removeClass().addClass('updnValidationErrorLabel');        
  }
});

Any help will be greatly appreciated.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
user420054
  • 65
  • 4
  • 14
  • -1 for badly formatted, unreadable code in the question. – Hari Pachuveetil Sep 04 '10 at 00:01
  • But still cannot figure what are you trying to achieve here... :-\ – Hari Pachuveetil Sep 04 '10 at 01:01
  • Hi, Sorry for not able to conveying it properly. I have label which is associated with two input controls. I have similar structure through out the page. My validator will set either of the input controls to updnValidationErrorInput class when something is wrong. I m trying to make label associated with the input controls with updnValidationErrorLabel class when either of input is wrong. And common Label is determined if it is with in tr. – user420054 Sep 04 '10 at 01:09

1 Answers1

0

Since you're coming from the <input> the .length checks must be true, so you can remove them. You can shorten it down to this overall:

$('.updnValidationErrorInput').each(function() {
   $(this).cloesest('tr').find('label[for]')
          .removeClass().addClass('updnValidationErrorLabel');
});

Instead of .parents('tr:first') you can use the equivalent (and cheaper) .closest(), then find any <label> elements with for="" attribute and change the class on them.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Hi nick, what is the difference between `.parents()` & `closest()` ?? –  Sep 04 '10 at 06:00
  • @Avinash - `.parents()` crawls all parents and *then* filters if given a selector, giving it a `:first` makes it get the first one since they're in the order of going up the DOM. `.closest()` crawls up the DOM and stops as soon as it finds a selector match, same result in this case, more efficient approach. The more parents, the more savings with `.closest()`. – Nick Craver Sep 04 '10 at 09:42