Enclose all elements within a pair of parent tags

Currently, I am dealing with multiple rows that have a fixed structure and attributes. However, I have the flexibility to insert child elements (.start & .end) within these rows.

My goal is to enclose all elements following a row with a .start child element until the subsequent row contains an .end child element. To achieve this, I designed an iteration using the each method. Unfortunately, I encountered an issue where the closing div of my wrapping is nested too deeply.

In the code snippet below, you can observe that the border encompasses all elements after the .start element, when it should only surround rows 3, 4, and 9.

$('.row .start').closest('.row').each(function (index) {
  $(this).nextUntil(".row .end").wrapAll("<div class='box'/>");
});
.box{
  border: 1px solid red;
  background-color:pink;
  padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="element">1</div>
</div>
<div class="row">
  <div class="element">
    <div class="start">start</div>
  </div>
</div>
<div class="row">
  <div class="element">3</div>
</div>
<div class="row">
  <div class="element">4</div>
</div>
<div class="row">
  <div class="element">
    <div class="end">end</div>
  </div>
</div>
<div class="row">
  <div class="element">6</div>
</div>
<div class="row">
  <div class="element">7</div>
</div>
<div class="row">
  <div class="element">
     <div class="start">start</div>
  </div>
</div>
<div class="row">
  <div class="element">9</div>
</div>
<div class="row">
  <div class="element">
    <div class="end">end</div>
  </div>
</div>

Answer №1

Utilize the :has() selector

$(this).nextUntil(".row:has('.end')")

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to utilize the function body onload in jQuery?

I have some code that needs to be fixed. Currently, when I see <body onload="start()" onresize="resize()" onorientationchange="resize()">, I want the following code snippet to work: $("button").click(function(){ $("body").onload = start; or win ...

Encountering a NextJS error when trying to extend with react-three-fiber due to window not

Encountering an Unhandled Runtime Error when running the code, specifically with the issue of "window not defined". The website still functions properly despite the error, but the build process fails. Below is the code snippet in question: "use clien ...

Restrict a class to contain only functions that have a defined signature

Within my application, I have various classes dedicated to generating XML strings. Each of these classes contains specific methods that take input arguments and produce a string output. In order to enforce this structure and prevent the addition of methods ...

What is the reason for the automatic loading of coffee files in app/assets/javascripts across all files?

I have a question about organizing my Javascript files in my app. I have multiple files in the app/assets/javascripts folder, and for testing, I have written some code in a file named test.coffee: $ -> $(document).ready -> alert("Hey"); When ...

Uncovering the inherent worth of an item pulled from a remote location using Vue.js

Currently, I am retrieving a list of countries by making an API call in VueX. The fetched data is then stored in a state using a mutation. Essentially, the countries are saved in a variable known as this.$state.getters.countryList, which can be accessed f ...

Use Javascript to set cookies and remember the show state when clicked

Can you offer some advice? I am looking to add cookies to my simple JavaScript code. Currently, the div is displayed when clicking on a link, but it hides again when the page reloads. I would like to implement cookies to remember the show state for 7 days. ...

Send the data to the web form along with the JSON information

I find myself in a situation where I am looking for alternative ways to pass data to a redirect page without using querystrings. Here is the current implementation on the Source Page: window.location = "invaliduser.aspx?u=" + encodeURI(username); Consi ...

When utilizing var_dump in PHP, the SQL query may return an empty array with an output of array(0

I am puzzled as to why my Query is not retrieving any data, despite its title My JQuery: $(".output").click(function() { var noteid = $(this).data("noteid"); $("#right-box").load("connectionDetails.php", { noteid: noteid }); }); My connectionDet ...

Retrieving information from controller to HTML page in AngularJS

Check out my code on Plunkr: http://plnkr.co/edit/8sBafktFzFa8fCLLJgMF This is the JavaScript file: angular.module('ui.bootstrap.demo', ['ui.bootstrap']); angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl&ap ...

"What is the best way to send back multiple responses to an AJAX request in a Struts action class

Looking for some guidance on handling responses in an Ajax call from a Struts (1.3.10) action class. I've been using the PrintWriter class to send responses back to the client's browser, and while it works sometimes, there are instances where the ...

Header element not keeping the navbar at the bottom

My goal is to attach this navigation bar to the bottom of a header, but it's sticking to the top instead. I want the navigation to remain at the bottom of the header even when the user scrolls down. Currently, both the header and navigation are set t ...

Mistake while defining a global variable within an HTML file and utilizing it

In my HTML document, I have set a global variable like this: <script type="text/javascript"> var total; </script> However, when I try to use this variable, I encounter an error stating that it cannot be used in this context. To resolve this ...

What is the process for modifying the heading color in Bootstrap's dark mode?

If you want to enable dark mode according to the Bootstrap documentation instructions, follow these steps: <html lang="en" data-bs-theme="dark"> <head> <link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel= ...

Guide to implementing proportional height for an element using JQuery 3.3.1

I am currently working on dynamically setting the heights of elements with a specific class using JQuery in order to achieve a 16:10 ratio based on their width. However, I'm encountering an issue where the height doesn't seem to be applied and re ...

Can an icon be included in Material UI's DataGrid headers when the sorting direction is not defined?

In the DataGrid's API of Material UI, you can see how to include a sort icon for ascending and descending directions. By default, these icons are shown as arrow up and arrow down symbols but can be customized using props. However, my project requires ...

Combining Files with Gulp.js and Handling File Names

I need a solution that will add the file name as a comment to each file in the stream. This means creating a comment line with the file path in the final destination file. The current functionality is as follows: pseudocode: concat(file1, file2) # outpu ...

Can someone explain how to trigger a JavaScript confirmation dialog box before the page loads in an ASP.NET application?

Below is the server side code snippet I am using: protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { string confirmValue = Request.Form["confirm_value"]; if (confirmVal ...

Choosing a date from a jQuery datepicker-Ui with Selenium WebDriver

Is there a way for Selenium web driver to choose a specific date from a jQuery datepicker-UI and input it into a date field? 1) I attempted using Jscript to set the date, but it seems that the date must be chosen from the jQuery pop-up window for the form ...

What are the steps for expanding the functionality of the sails.js library?

I am interested in expanding sailsjs to incorporate a feature similar to rails strong params, like this: req.params.limit(["email", "password", "password_confirmation"]) I have already created an addon for this, but I want it to automatically connect to ...

Looking for a solution to fix the VueJS calculator that only works once?

My calculator project using VueJS, HTML and CSS is almost complete. However, I'm facing an issue where it only works once. For example, if I input 6x3, it correctly gives me 18. But if I then clear the result and try to input a new calculation like 3 ...