Circle B
Circle B

Reputation: 1674

jQuery .find and .nextUntil Together

I'm using ajax to get the contents of an external file, and then grabbing one element from the string and loading that into an element on my page using jQuery's find(). This is working great, but now I'm needing to get everything between one element and another from the external file, I've tried using .nextUntil(), but it's not stopping at the right element, and is grabbing the entire rest of the document. So the question is, can I do this, or is there another way I should do it?

...
success: function(result) {
     html = $(result),
         comp = $(html).find("#comp1").nextUntil(".comp-close");
     $('.output-text').html(comp);
},
...

Upvotes: 1

Views: 862

Answers (1)

phenxd
phenxd

Reputation: 689

Your problem is probably that ".comp-close" is not a direct sibling.

I validated this behavior here :

<div id="1"></div>
<p>1</p>
<p>2</p>
<p>3</p>
<div>
  <p>4</p>
  <div id="2"></div>
  <p>5</p>
</div>
<div id="3"></div>
<p>6</p>

<p>=====</p>
<div id="4"></div>

// Clones until the end of the document
//var list = $('#1').nextUntil("#2");

// Clones until #3 since it is a direct sibling
var list = $('#1').nextUntil("#3");

console.log(list)

// Clone if you do not want to remove the originals from the DOM
$('#4').append(list.clone());

JSFiddle

Also note that you should use clone() if you do not want to modify the DOM.

Hope this helped!

Upvotes: 1

Related Questions