0

I want to get the exact HTML of the element I want

like if I have this:

<div class="input">
  <input type="number" id="nanometer" autocomplete="off" class="field" placeholder="nm" oninput="exchange()">
</div>

I want to get that exact HTML code in my JavaScript:

var nano = document.getElementById("nano")

and now I expect nano to be:

<input type="number" id="nano" autocomplete="off" class="field" placeholder="nm" oninput="exchange()">

and for my work, I have to make it duplicate itself like this:

<input type="number" id="nano" autocomplete="off" class="field" placeholder="nm" oninput="exchange()">
<input type="number" id="nano" autocomplete="off" class="field" placeholder="nm" oninput="exchange()">

by this code:

document.getElementById(nano).outerHTML += nano

but it wont work

7
  • var html = document.getElementById("nano").outerHTML ? isn't it? But remember, id must be unique, if you duplicate the element with same id, you'll have invalid HTML Commented Feb 3, 2020 at 18:46
  • this var html = document.getElementById(nano).outerHTML gets the whole html codes in the <div> bu i want it to get just it self, and i doesnt matter if the id gets copied, because if i make the javascript code o it, i have plans to change the ids in another copies Commented Feb 3, 2020 at 18:48
  • 1
    which div? Please, share with us your current relevant code, take a look at minimal reproducible example Commented Feb 3, 2020 at 18:49
  • 1
    @Rawand id values must be unique through the entire document; you cannot re-use ids. Commented Feb 3, 2020 at 18:50
  • You might ask about what you're actually trying to do rather than this specific operation. See the XY problem. Commented Feb 3, 2020 at 18:52

2 Answers 2

1

Although the question is for making a clone using HTML strings, it is better to not work with HTML strings but with DOM elements instead. Those are the objects that a browser uses internally.

Using DOM elements it is much easier to control the results, esp. if you need to make changes to the new item - such as giving it a new id, which is mandatory because every id on every page should be unique.

Here is an example:

var nano = document.getElementById("nano");
var parent = nano.parentNode;

var clone = nano.cloneNode(true);
clone.id = "newNano";
clone.setAttribute("value", 123);

parent.insertBefore(clone, nano.nextSibling);
<input type="number" id="nano" autocomplete="off" class="field" placeholder="nm" oninput="exchange()">

Sign up to request clarification or add additional context in comments.

Comments

0

First, get the outer HTML. Then, to duplicate it, you have to append the new HTML to the parent node of whatever you're trying to get. Also, you would need to replace the new ID with something unique. So it would be:

    var nano = document.getElementById("nano"),
        outer = nano.outerHTML,
        newOuter = outer.replace(
          'id="',
          'id="' + Date.now()
        ),
        parent = nano.parentNode;
    
    parent.innerHTML += newOuter;
    console.log(parent.innerHTML)
<div>
<input type="number" id="nano" autocomplete="off" class="field" placeholder="nm" oninput="exchange()">
</div>

1 Comment

@CalvinNunes OK so just add anotehr line newouterHTML = outer.replace("id=", "id='" + Date.now() + "')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.