Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-09-2009
Member
 
Join Date: Jun 2009
Posts: 3,859
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,countrycountry
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();
          });
         }
        });
 });

Reply With Quote
  #2  
Old 28-09-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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); 
Reply With Quote
  #3  
Old 28-09-2009
Member
 
Join Date: Jun 2009
Posts: 3,859
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();}); 
   
}); 
Reply With Quote
  #4  
Old 28-09-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "jQuery and several events"
Thread Thread Starter Forum Replies Last Post
Updates with jQuery 1.6.1 Vincent D Software Development 5 23-06-2011 10:17 PM
New features in jQuery 1.4.3 Botan Software Development 6 03-12-2010 06:43 AM
Read XML and RSS with jQuery Ground 0 Software Development 3 27-11-2010 06:18 AM
Jquery or Perl CLONE Software Development 1 13-05-2010 01:30 AM
Manipulate jQuery with PHP Zecho Software Development 5 16-06-2009 02:14 AM


All times are GMT +5.5. The time now is 03:01 AM.