jquery - Traversing up the DOM and removing a class -
i have several forms on page, each form has it's own "message box" displays validation error. each "message box" has close button. have no issue getting close button close "message box" need remove class "error" offending text input. page content brought in ajax, hence ".on"
pseudo html (remember there @ time 8 of these wrapped in own form tag, message box , closing button part of.
<form action="#" method="post" id="form6"> <ul> <li> <label for="foo">label</label> <input type="text" name="foo" id="foo"> <input type="submit" id="button6" value="submit" /> </li> </ul> <div id="msg6" class="error"><a class="close">x</a><ul></ul></div> </form>
my attempt
$('body').on('click', 'a.close', function(){ $(this).parent().fadeout("fast"); $(this).closest('input[type="text"].error').removeclass('error'); // doesn't work $('a.close').closest('ul > li > input.error').remove();// doesn't work //console.log($.closest('input[type="text"].error'));//tring log value of ancestor });
this should work:
$(this).parents('form').find('input.error').removeclass('error');
but if want remove it, no need alter class, utilize .remove()
...
jquery dom dom-traversal
No comments:
Post a Comment