Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript insertAdjacentHTML not working

In fiddle - http://jsfiddle.net/u3bmytnL/, does not work as expected

var x = document.createElement("button");
x.textContent = "byyyyy";
elx = document.getElementById("el");
elx.insertAdjacentHTML('afterend', x); 
like image 776
nikhil rao Avatar asked Jul 05 '26 03:07

nikhil rao


1 Answers

That's should resolve:

var x = document.createElement("BUTTON");
var text = document.createTextNode("byyyy");
x.appendChild(text);
elx = document.getElementById("el");
elx.insertAdjacentHTML('afterend', x.outerHTML); 

The insertAdjacentHTML needs a string as second parameter, and you was passing a DOM element, so you need to get the HTML string

like image 193
Francesco Avatar answered Jul 06 '26 15:07

Francesco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!