Answer the question
In order to leave comments, you need to log in
How to pass a value to a function in javascript?
Hello everyone
Guys, plz tell me how to pass the current (!) Value of an element to a function?
That is, there are buttons whose values are formed dynamically. And you need to pass these values in javascript.
I
found
this example: function. plz tell me
thanks
Answer the question
In order to leave comments, you need to log in
Don't use onclick on a tag unless absolutely necessary.
function test(data) {
console.log(this.innerHTML);
}
var btn = document.getElementById('btn');
btn.addEventListener('click', test, false);
It is possible like this:
But according to the rules of good manners, it’s better not to set onclick in the tag as an attribute, but hang the event on the element, html separately, js separately, and it’s better not to use global functions, but to do something like myAppObject.myFunctionName(this):
Javascript :
document.getElementById('click-here').addEventListener('click', displaySomething);
function displaySomething() {
console.log(this); // элемент, на который нажали
alert('Hi');
}
$('#click-here').on('click', function(e) {
e.preventDefault();
console.log($(this)); // jQuery элемент, на который нажали
alert('Hi');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question