coiso
coiso

Reputation: 7479

.keyup() is only working once, why?

I am using this pretty simple jquery function, but it seems to work only on the first keyup..

$('#cmentuser').keyup(function() {
 var mess = document.getElementById('cmentuser').value;
 var dataString = 'message='+ mess; 

    $.ajax({  
  type: "POST",  
  url: "atuamae.org/comentbyuser.php",  
  data: dataString,  
  success: function() {  
 }  
});
});

any ideas on how to keep it active?

Upvotes: 1

Views: 2588

Answers (3)

Punit
Punit

Reputation: 1120

try this

$('#cmentuser').live('keyup',function() {
 var mess = $(this).val();
 var dataString = 'message='+ mess; 
    $.ajax({  
  type: "POST",  
  url: "atuamae.org/comentbyuser.php",  
  data: dataString,  
  success: function() {  
 }  
});
});

Upvotes: 0

adeneo
adeneo

Reputation: 318182

$(document).on('keyup', '#cmentuser', function(e) {//try to find lower element then doc
   var dataString = 'message='+ $(e.target).val();

   $.ajax({  
      type: "POST",  
      url: "/comentbyuser.php", //no cross domain requests, no need for domain name
      data: dataString,  
      success: function() {}  
   });
});

Upvotes: 0

Tadeck
Tadeck

Reputation: 137320

It works, also in the following form (changed mess into jQuery(this).val() and relied on jQuery when encoding the data string):

$('#cmentuser').keyup(function() {
    $.ajax({  
        type: "POST",  
        url: "atuamae.org/comentbyuser.php",  
        data: {
            'message': jQuery(this).val()
        },  
        success: function() {
            // success callback
        }  
    });
});

Proof that it works: jsfiddle.net/xfxPR/

You may be dynamically changing some elements (eg. changing ID or assuming id does not need to be unique), or maybe unbinding the event. Just make sure the event is being attached and stays attached to the element you need.

Upvotes: 3

Related Questions