It is possible yes. See what it says to jQuery change method documentation.
Example #1 - passing function name:
function teste(ev){
alert($("select").val());
}
$(document).ready(function() {
$("select").change(teste); // aqui passo o nome da função a ser executada
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option value="1">valor 1</option>
<option value="2">valor 2</option>
<option value="3">valor 3</option>
</select>
Example #2 - anonymous function:
$(document).ready(function() {
$("select").change(function(ev){ // pode ser arrow function também: (ev) => alert($("select").val());
alert($("select").val());
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option value="1">valor 1</option>
<option value="2">valor 2</option>
<option value="3">valor 3</option>
</select>
Example #3 - passing function-specific data:
$(document).ready(function() {
$("select").change(
'Você selecionou o valor: ', // aqui eu defino o que quero passar pra função
(ev) => alert(ev.data+$("select").val()) // com ev.data eu recupero o que passei por argumento
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option value="1">valor 1</option>
<option value="2">valor 2</option>
<option value="3">valor 3</option>
</select>
Note that passes to string
'Você selecionou o valor: '
which could be any other type, such as an object, for example...
What result do you want to get? Explain better.
– Pedro Henrique
use
$("select").change(teste);
instead of$("select").change(teste());
.– Benilson
It worked Benilson, but what if I want to send a video to my function, how would I do it? for example:
$("select").change(teste('par1'));
– Thiago Costa
Uses an anonymous function by receiving the parameter and running internally
teste(parametro)
.– Benilson