I'm trying to check if the DOM contains an element with ID = "htmlFileLink."
let element = document.createElement('a');
element.id = 'htmlFileLink';
document.body.insertAdjacentHTML( 'beforeend', element );
if( document.body.contains( document.getElementById('htmlFileLink') ) ) {
alert ('yes');
}
else {
alert ('no');
}
Why is it alerting "no" instead of "yes?"
insertAdjacentHTML
waits for a HTML string as an argument, not an element. UseappendChild
to append new elements to existing elements. – Softcover