0

I have been through:

  1. Wrap plain text in paragraph Jquery
  2. Wrap text within element
  3. Wrap element between two elements in JavaScript/jQuery?
  4. jQuery wrapAll contents including text between two tags
  5. Get the text after span element using jquery none of those provide a solution for what I am trying to do. I think adding the div may be part of the reason.

How can I enclose the content between div_1 and div_2 in a div_3

<div id="div_1"></div>
<strong>stuff here that always begins with same string but does not end that way</strong>
some unknown text here
<div id="div_2"></div>

The real issue is that it seems you cannot insert the beginning of a div without the end of div showing up automatically.

For example, if I do something like this:

$(document).ready(function(){
$('#div_1').after('<div id="div_3">');
$('#div_2').before('</div>');
});

div_3 gets inserted after div_1 but with a closing tag i.e.

<div id="div_1"></div>
<div id="div_3"></div>
    <strong>stuff here that always begins with same string but does not end that way</strong>
    some text here with no consistent string
    <div id="div_2"></div>
ACKmiecik
  • 70
  • 8
  • 1
    The problem is that you're thinking in HTML and not in DOM. – freedomn-m Mar 10 '23 at 12:52
  • Please take the [tour]. Answers do not belong in your question. – isherwood Mar 10 '23 at 13:59
  • Protip: Just about anything you can wish to do with jQuery has been covered on SO already. Also, a regular review of the [methods available to you](https://api.jquery.com/) will give good hints about how to tackle a particular challenge. – isherwood Mar 10 '23 at 14:04
  • NONE of the "This question already has answers here" pertain to my situation. I had been through all of them already – ACKmiecik Mar 10 '23 at 14:40

1 Answers1

1

You can enclose the content between div_1 and div_2 inside a new div element using the following jQuery code:

$('#div_1').nextUntil('#div_2').wrapAll('<div id="dev_3"></div>');
  • Sweet, thanks. Now I just discovered the 'some unknown text here' has a variable amount of
    tags and all those get bunched up after the content. So `code`
    stuff here that always begins with same string but does not end that way some text here
    more stuff
    even more stuff
    `code` becomes 'code'
    stuff here that always begins with same string but does not end that way

    `code`
    – ACKmiecik Mar 10 '23 at 13:49