Parameter passing, ele.on ('click', func);

2

How do I get parameters passed as follows?

function hello(msg) {
    alert(msg);
}
$('ele').on('click', hello);

How can I pass the msg to the hello(); function

The alternative with native javascript is also welcome.

    
asked by anonymous 17.08.2017 / 16:34

2 answers

1

You can pass the parameter in sequence, and it will be added to the date within the event parameter:

function hello(event) {
    alert(event.data.msg);
}

$('#ele').on('click', {msg: 'Teste'}, hello);

Fiddle

    
17.08.2017 / 16:42
0

I did it this way, I do not know if it's the right one but:

function hello(msg) {
    alert(msg);
}
$('ele').on('click', function() { hello('hello') });

function hello(msg) {
    alert(msg);
}
$('button').on('click', function() { hello('hello') });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Hello</button>
    
17.08.2017 / 16:40