Results 1 to 4 of 4

Thread: jQuery and several events

  1. #1
    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();
              });
             }
            });
     });


  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

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

  3. #3
    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();}); 
       
    }); 

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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?

Similar Threads

  1. Updates with jQuery 1.6.1
    By Vincent D in forum Software Development
    Replies: 5
    Last Post: 23-06-2011, 10:17 PM
  2. New features in jQuery 1.4.3
    By Botan in forum Software Development
    Replies: 6
    Last Post: 03-12-2010, 06:43 AM
  3. Read XML and RSS with jQuery
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 27-11-2010, 06:18 AM
  4. Jquery or Perl
    By CLONE in forum Software Development
    Replies: 1
    Last Post: 13-05-2010, 01:30 AM
  5. Manipulate jQuery with PHP
    By Zecho in forum Software Development
    Replies: 5
    Last Post: 16-06-2009, 02:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,225,869.43349 seconds with 16 queries