Elena
Elena

Reputation: 9

Jquery select text after a div without id or class

Here is my html code:

<div>
    Please complete this form to create a new account.
    <br>
    Fields marked  with (*) are required.
    <div id="errorZone"></div>
</div>

I want to select text between div and div id="errorZone".

I'm trying this code:

$('#errorZone').prev().text().remove();

But this doesn't work

Upvotes: 0

Views: 176

Answers (3)

SajuPK
SajuPK

Reputation: 97

may be you can use the .parent() instead of .prev(). See this link:- How to get the text of parent element using jquery?

Upvotes: 0

M K Garwa
M K Garwa

Reputation: 495

$('#errorZone').closest('div:not('.errorZone')').remove();

Upvotes: 0

David Thomas
David Thomas

Reputation: 253506

If you want to remove everything (including the <br />):

var start = document.getElementById('errorZone');
while (start.previousSibling) {
    start.parentNode.removeChild(start.previousSibling);
}

var start = document.getElementById('errorZone');
while (start.previousSibling) {
  start.parentNode.removeChild(start.previousSibling);
}
<div>
  Please complete this form to create a new account.
  <br>Fields marked with (*) are required.
  <div id="errorZone">Text in 'errorZone.'</div>
</div>

Upvotes: 4

Related Questions