views:

77

answers:

3

How to send triimed data using $.get() see code snippet below:

$("#txtSearch").keyup(function() {
  $.get("/controller/method/",
     { 'param1': $.trim($("#txtSearch").val()) !== "" ? $.trim($("#txtSearch").val()) : return false; },
     function(data) {
         $("#div").html(data);
  });
});

This results in syntax error whether or not i give semi colon(":") after return false. So i just want to ask should i use var & assign txtBox data to var & then serach if yes then how to use var after giving 1st parameter i.e url path in $.get as i am using it inside of keyup function so i have to trim data inside of $.get.

+1  A: 

Call $.get after checking the value of the textbox.

$("#txtSearch").keyup(function() {
    var textVal = $.trim($("#txtSearch").val();
    if ( textVal !== "" )
    {
        // call your $.get function here.
    }
    else
    {
        return false;
    }
});
rahul
+1  A: 

I use this small utility function:

jQuery.fn.trimAndSerialise = function () 
{
    this.find('input, textarea').each(function()
    {
        $(this).val(jQuery.trim($(this).val()));
    });
    return this.serialize();
}

Then use it like this:

var f = $('#aForm'); 
var data = f.trimAndSerialise();

P.S. I haven't tested this with $.get though (I normally use $.ajax...)

+1  A: 

This seems the most sensible:

$("#txtSearch").keyup(function() {
    var strSearchVal = $.trim($("#txtSearch").val()); 
    if (strSearchVal !== "")
    {
      $.get("/controller/method/",
     { 'param1': strSearchVal},
     function(data) {
         $("#div").html(data);
  });
}
    else
    {
        return false;
    }
    });
Keir