N
N
nano_e_t_42015-10-23 23:35:17
JavaScript
nano_e_t_4, 2015-10-23 23:35:17

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

3 answer(s)
A
Alexander Aksentiev, 2015-10-23
@nano_e_t_4

https://jsfiddle.net/k32xuuo7/

K
keslo, 2015-10-23
@keslo

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);

By the way, your example is working.

R
Radiks Alijevs, 2015-10-23
@dedalik

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');
}

If you're using jQuery it's much easier:
jQuery:
$('#click-here').on('click', function(e) {
    e.preventDefault();
    console.log($(this)); // jQuery элемент, на который нажали
    alert('Hi');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question