10

Having trouble with a function hitting the page as soon as possible, so i need to write it in pure javascript and include it in the head. not sure how to go about it, because as i understand it, by using .replace() the new element will be moved to a different location on the page. jQuery's replaceWith() behavior is ideal.

$("#imagefiles").replaceWith("<input type='file' name='imagefiles' id='imagefiles' />");
2
  • 1
    What is the problem with using jQuery again? Commented Apr 15, 2012 at 19:27
  • 1
    .replace() is for strings. Please don't use it for DOM. Could you describe your issue a little better? Are you saying you want an element to be replaced as soon as it appears, and not wait for the rest of the DOM to be ready? Commented Apr 15, 2012 at 19:28

3 Answers 3

9
var image = document.getElementById('imagefiles'), parent = image.parentNode,
tempDiv = document.createElement('div');

tempDiv.innerHTML = "<input type='file' name='imagefiles' id='imagefiles' />"

var input = tempDiv.childNodes[0];

parent.replaceChild(input, image);

DEMO


EDIT as per am not i am:

var image = document.getElementById('imagefiles'), parent = image.parentNode,
input = document.createElement('input');
input.id = input.name = "imagefiles";
input.type = 'file';

parent.replaceChild(input, image);

edited DEMO

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

4 Comments

Why the div? It'll be faster and less code to just create the input directly. input = document.createElement('input'); input.type = "file"; input.id = input.name = "imagefiles";
@qwertymk it works in jsFiddle, but my webinspector is showing an error on the final line: "Uncaught SyntaxError: Unexpected token ILLEGAL"
@technopeasant: dude, you gotta vote more to get people to answer you, 11 votes all time when you asked 128 questions?
@technopeasant: You can pick up some invisible and illegal characters if you copy/paste from jsFiddle. Better to copy the code from here instead.
3
  const node = new DOMParser().parseFromString(html, 'text/html').body.childNodes[0];
  document.getElementById(id).replaceWith(node);

Comments

1
let element = document.getElementById("imagefiles");
element.insertAdjacentHTML("afterend","<input type='file' name='imagefiles' id='imagefiles' />");
element.parentNode.removeChild(element);

Years later. There is now an childNode.replaceWith() but it is not strictly equivalent to the JQuery replaceWith(); Nor is it compatible with I.E.

The above code creates a document fragment from the given HTML, and inserts it after the original element in the DOM and then deletes the original element, effectively replacing it.

https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith

1 Comment

This I thought was the simplest way to emulate jQuery's replaceWith. No temp element needed.

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.