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