A proper solution would be to give the .cloneNode()
a new ID, and only apply CSS to the element's class rather than its ID. So:
var clone = document.getElementById("rec_search_input").cloneNode();
clone.id = "some other id";
...and then append that elsewhere in the DOM (e.g., via appendChild
, insertBefore
, etc.). (Side note: Since your element is an input
, it can't have children, but if you use this for other things, you can use cloneNode(true)
to clone its descendant elements; then spin through them to be sure their id
values are unique.)
To some extent, you can "cheat". ID uniqueness isn't enforced by the browser or anything, and CSS will apply to elements with the same ID even if there are more than one. Demo
However, as you are probably aware, JavaScript functions like getElementById
will not work predictably if you have duplicate IDs.