jQuery and several events
I recently began to use the jQuery framework. But this morning I encountered a problem. I do not know how to run a function on multiple events. In my form I would like to make a function on a click in a select or keyup on an input text. To solve my problem I went through a function and I call this function on my click and select onkeyup input in my area. I wonder if it is possible to avoid these function calls in the html code with jquery?
Code which is not OK. My code does not fire on the keyup event as I would like keyup on id_coclico and click on the country id.
PHP Code:
$ (document). ready (function () {
var id = $ ( '# id_coclico, #country');
$. each (id, function () {
$ (this ).keyup(function(event){
coclico = $("#id_coclico" ).val();
country = $ ( "# country"). val ();
$.post('ajax/ajax_test_coclico.php',{
coclico:coclico,country: country)
function(data){
if (data == '1 ') {
$ ( "#test_nok"). hide ();
$('#test_ok').fadeIn("slow", 0, function(){
$ ("#test_ok"). show ();
});
}
else
{
$ ( "# test_ok"). hide ();
$ ( '# test_nok'). FadeIn ( "slow", 0, function () {
$ ( "# test_nok"). show ();
});
}
});
});
});
});
Code is ok but not really what I want because I have to call a function in my form:
PHP Code:
function test_client(){
var id = $('#id_coclico,#country');
$.each(id, function() {
coclico = $("#id_coclico" ).val();
country= $("#country" ).val();
$.post('ajax/ajax_test_coclico.php',{
coclico:coclico,country:country},
function(data){
if (data=='1'){
$("#test_nok" ).hide();
$('#test_ok').fadeIn("slow", 0, function(){
$("#test_ok" ).show();
});
}
else
{
$("#test_ok" ).hide();
$('#test_nok').fadeIn("slow", 0, function(){
$("#test_nok" ).show();
});
}
});
});
}
Re: jQuery and several events
I didn't understood what you want. In your second code, are not events that's normal? Why do you not put something like:
PHP Code:
$("your_select" ).click(name_your_function);
$("your_input_text" ).keyup(name_your_function);
Re: jQuery and several events
The second code I call with onClick = "test_client ()" and onkeyup = "test_client ()" in my form and I would like to avoid this up as jQuery allows to pass.
And I tried your solution and it works well. On the other side, there is no function in jQuery which allows the simultaneous events?
PHP Code:
function test_client(){
var id = $('#id_coclico,#country');
$.each(id, function(){
coclico = $("#id_coclico" ).val();
country= $("#country" ).val();
$.post('ajax/ajax_test_coclico.php',{
coclico:coclico,country:country},
function(data){
if (data=='1'){
$('#test_ok').fadeIn("slow", 0, function(){
$("#test_ok" ).show();
});
$("#test_nok" ).hide();
}
else
{
$("#test_ok" ).hide();
$('#test_nok').fadeIn("slow", 0, function(){
$("#test_nok" ).show();
});
}
});
});
}
$(function(){
$("#id_coclico" ).keyup(function(event){test_client();});
$("#country" ).click(function(event){test_client();});
});
Re: jQuery and several events
What do you mean by "simultaneous events"? It works the same way as what you put, but it's more readable:
PHP Code:
$("#id_coclico" ).keyup(test_client);
$("#country" ).click(test_client);
And if not, you put "$(function(){" you meant "$(document).ready(function(){" I suppose?