3

I am using JQuery and I have got below JQuery Code sample.

JQuery Code:

 $.ajax({  
        type: "POST",
        url: "Login.aspx",  // Send the login info to this page
        data: str,  
        success: function(result)
        {  
             // Show 'Submit' Button
            $('#loginButton').show();

            // Hide Gif Spinning Rotator
            $('#ajaxloading').hide();  

            var resLength = (result).val().trim().length;
            alert(resLength);
            if(resLength!=0)
            {

                var arr = result.split(",");
                var fname = arr[0];
                var lname = arr[1];
                var activeCardNo = arr[2];
                var multipleTier = arr[3];
                var activeStatus = arr[4];
                var access = arr[5];
            }
        }
    }); 

In Above code sample when I am trying to use .val() in below line

var resLength = (result).val().trim().length;

it is giving error "result.val is not a function", If I am using just result.trim().length its working fine in firefox, however giving error in IE.

Please suggest!

2
  • By the way - your code can be much cleaner if you used JSON instead of a comma separated string.
    – Kobi
    Commented Dec 20, 2010 at 6:05
  • can you please suggest how can I use it, I have no Idea of JSONP..thanks Commented Dec 20, 2010 at 12:02

5 Answers 5

6

try this:

var resLength = $.trim(result).length;

If result is a string, it doesn't have a val function. trim isn't supported cross-browser, so you should use jQuery.trim instead.

Another option is checking the value directly. The empty string has a false value in JavaScript, so you can check:

result = $.trim(result);
if(result)
{
    // split, ...
}

If result is intended to be an element, you should wrap it in a jQuery object:

var resLength = $.trim($(result).val()).length;
1
  • very nice detailing of the answer. Commented Dec 21, 2010 at 5:43
0

The correct syntax is result.trim().length and IE doesn't support trim() (see .trim() in JavaScript not working in IE) so it complains.

Try adding the accepted answer code from he linked question to your code and see if it fixes it.

0

The code should be:-

var resLength = result.val().trim().length;
alert(resLength);

Writing "(result)" instead of "result" gives the error. This is because the variable here is the "result" itself.

Hope it helps.

1
  • I can't reproduce this, and can't think of a simple scenario where ( ) around a variable would make that difference.
    – Kobi
    Commented Dec 20, 2010 at 5:56
0

(result).val(); this is wrong

the above one is used for input type html values

if the result is html please use

.text(); or .html()
0

It's not a jQuery object, it's just a text value of the returned ajax

Not the answer you're looking for? Browse other questions tagged or ask your own question.