Passing parameters, he.on('click', func);

Asked

Viewed 466 times

2

How do I get past parameters the following way?

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

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

The alternative with native javascript is also welcome.

2 answers

1

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

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

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

Fiddle

0

I got it this way, I don’t know if it’s right 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.