Skip to content Skip to sidebar Skip to footer

How To Get Element In Template Html?

I am developing an Electron app, in which the index.html imports a search.html: And inside the search.html, I create a button whose id

Solution 1:

Run the querySelector function on the templates content property.

var link = document.querySelector('link[rel="import"][href="search.html"]');
var templateContent = link.import.querySelector('template');
console.log(templateContent);
var searchBtn = templateContent.content.querySelector('#searchBtn');  
console.log(searchBtn);

More about document fragment.

More about template.

Working sandbox;

Solution 2:

Try like this :

// parse you HTML firstvar html = link.import.querySelector('template');
var parser = new DOMParser();
var doc = parser.parseFromString(html.content, "application/xml");

//latervar searchBtn = doc.querySelector('#searchBtn');  
console.log(searchBtn);

Post a Comment for "How To Get Element In Template Html?"