开发者

jquery set class for a children of particular type

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

<tr>
  开发者_Python百科  <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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜