I'm trying to implement the same code of this using JavaScript, so wrote the below code as btn.js file:
// Create a new object based of the HTMLElement prototype
var SaveBtn = Object.create(HTMLElement.prototype);
// Set up the element.
SaveBtn.createdCallback = function() {
// Create a Shadow Root
var shadow = this.createShadowRoot();
this.type='button';
// Add an event listener to the image.
this.addEventListener('click', function(e) {
alert('got a click');
document.getElementById("ShowButton").value= "Hide Filter";
});
};
// Register the new element.
var Xbtn =document.registerElement('save-button', {
prototype: SaveBtn,
extends: 'input'
});
and called it in the other function main.js as below:
window.addEventListener("load", onLoad, false);
function onLoad() {
var host = document.querySelector('#host');
var el = new Xbtn();
host.appendChild(el);
}
this worked fine, and gave me blanket button.
now I want to pass the 'text' to be displayed into the button, I tried the below:
in the index.html I've:
<div id="host"></div>
<input id="ShowButton" type="button" value="Show Filter"/>
in main.js:
el.value = 'click me';
and in the btn.js:
this.value = this.getAttribute('text');
but failed, and the button still empty! considering that the other button value is changed upon the click
any thought?