I have an issue in my Javascript code with the eventlistener. The Eventlistener (click) is firing over the whole document and not only on the specific element.
external file:
function $(selector){
const self = {
element: document.querySelector(selector),
on: (event, callback) => {
document.addEventListener(event, callback)
}
}
return self;
}
function coy(){
let _this = this;
this.Text = (element) => {
$(element).on('click', function(){
alert(document.getElementById(element).value);
});
}
}
On page
<input type="text" id="text"/>
coy.Text('text');
Hope somebody can help me.
What a strange question.
document on line 5this that doesn't have the text() method?Coy before you are able to call text() on it."text" to the coy.text() method, but it seems that it's expecting a selector because it uses that argument in a call of the $ function.document.querySelectorconst $ = (selector) => {
return {
element: document.querySelector(selector),
on: function (event, cb) {
this.element.addEventListener(event, cb);
},
};
};
class Coy {
constructor() {
this.text = (element) => {
$(element).on('click', () => alert($(element).element.value));
};
}
}
const coy = new Coy();
coy.text('input[type="text"]');
<input type="text" />