Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include tags html inside div with Javascript

I have an html source code composed of several tags. Unfortunately these do not have a parent container, so I would like to add it via Javascript because I cannot act directly on the html structure.

Now, with this function I am adding tags div after other elements (input in my case).

function insertAfter(referenceNode, newNode) {
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

var newDiv = document.createElement("div");
newDiv.setAttribute('class', 'example');
newDiv.setAttribute('id', 'new_div');

var input = document.getElementById("element_3");
insertAfter(input, newDiv);

I get this, but that’s not what I’m trying to do.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<input type="checkbox" class="some_class" id="element_1">
<input type="checkbox" class="some_class" id="element_2">
<input type="checkbox" class="some_class" id="element_3">
<div class="exampmple" id="new_div"></div>

Instead, what I would like to achieve is this
Now I am relatively new to Javascript and am looking to learn new things as a fan. Is it possible to do what I have described with Js or jQuery? I appreciate any help, thanks for any replies.

<div class="exampmple" id="new_div">
  <input type="checkbox" class="some_class" id="element_1">
  <input type="checkbox" class="some_class" id="element_2">
  <input type="checkbox" class="some_class" id="element_3">
</div>

Update:
This is my actual html code, the previous one I wrote to simplify the question.

<label for="plugin_delete_me_shortcode_password">Password</label>
<input type="password" autocomplete="off" autofocus="autofocus" id="plugin_delete_me_shortcode_password" name="plugin_delete_me_shortcode_password">
<input type="checkbox" class="togglePw" id="pw_3" onclick="showPassword('plugin_delete_me_shortcode_password')">
<label for="pw_3" class="fa"></label>

>Solution :

This will work better

function insertAfter(referenceNode, newNode) {
  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
const newDiv = document.createElement("div");
newDiv.id = "new_div";
newDiv.classList.add("example");
const inputs = document.querySelectorAll(".some_class");
const lastInput = inputs[inputs.length - 1]
insertAfter(lastInput, newDiv);

inputs.forEach(inp => newDiv.append(inp))
<input type="checkbox" class="some_class" id="element_1">
<input type="checkbox" class="some_class" id="element_2">
<input type="checkbox" class="some_class" id="element_3">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading