Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove an element that is before the selected element

I'm doing some form error handling/manipulation and I have the need to remove an error div that is before the input that is being validated...

HTML:

<p><div class="textError"></div><input type="text" name="someInputField" /></p>

I'd like to remove the div.textError if the input field is validated. I tried this...

$(this).before('<div class="textError"></div>').remove('<div class="textError"></div>');

and no dice. any help would be greatly appreciated

like image 457
Derek Adair Avatar asked Nov 01 '25 13:11

Derek Adair


1 Answers

The before method adds something. To get a relative use the prev method. So something like this:

$(this)
  .prev('div.textError')
  .remove()
  .end()
.before('<div class="textError"></div>');
like image 168
g.d.d.c Avatar answered Nov 03 '25 03:11

g.d.d.c