1

Basically i am trying to create a AJAX script in Jquery. but the main problem is that i want to define the variables like this

$(document).ready(function () {
    var u = $('#username');
    var s = $('#server');
    var p = $('#password');

    $('#server').keyup(function () {
         $.post("connect.php", { s: s.value, u: u.value , p: p.value },
             function(data) {
             $('#hol').html('checking connection...' + u.value + " " + p.value + " " + s.value + " " + data);
                             }
                 ); 
   });

});

But that somehow doesn't work.... it seems rather odd that i have to define the variables with "this" instead of being able to assign them before hand.

the code that works is

$(document).ready(function () {

    $('#username').keyup(function () {
        var u = this;
            $('#password').keyup(function () {
            var p = this;
                   $('#server').keyup(function () {
                    var s = this;

                     $.post("connect.php", { s: s.value, u: u.value , p: p.value },
                     function(data) {
                        $('#hol').html('checking connection...' + u.value + " " + p.value + " " + s.value + " " + data);
                                         }); });
                 }); 
                  }); 
   });

so my question is basically "How do I make jquery catch the input in the text field and assign it to a variable? like in the first example??" or it doesnt have to be done in the long way?

3
  • Sending AJAX requests onkeyup is a bad idea. Add some debouncing code so it only happens if the user stops typing for some time (250ms might be good). Commented Jul 26, 2012 at 11:03
  • Shouldn't the selector for your first code block be $('#server, #username, #password') Commented Jul 26, 2012 at 11:05
  • The "code that works" doesn't really work: nesting keyup handler creation means if the user enters their password first for some reason then the #server keyup never gets set and the Ajax call doesn't happen. Commented Jul 26, 2012 at 11:24

6 Answers 6

5

You need to use .val() instead of .value:

{ s: s.val(), u: u.val() , p: p.val() }

as s,u,and p are jQuery objects, not DOM Elements.

Sign up to request clarification or add additional context in comments.

1 Comment

@JohnP Yes, but in that case, you will lose "magical" jQuery functionalities on s,u and p elements ).
1

Use .val();

var u = $('#username').val();
var s = $('#server').val();
var p = $('#password').val();

Comments

0

you can use in this manner

$(document).ready(function () {
    var u = $('#username');
    var s = $('#server');
    var p = $('#password');

    s.keyup(function () {
         $.post("connect.php", { s: s.val(), u: u.val() , p: p.val() },
             function(data) {
             $('#hol').html('checking connection...' + u.val() + " " + p.val() + " " + s.val() + " " + data);
                             }
                 ); 
   });

});

Comments

0

You are using jQuery selectors..

var u = $('#username');
var s = $('#server');
var p = $('#password');

and accessing values in javascript fashion....

u.value; //WRONG

if you are using jQuery then the right way is

u.val();

or

u.attr('value');

Same goes for v and w :)

1 Comment

"and accessing values in javascript fashion" - should be "accessing values in DOM element fashion". Every part of the OP's code including jQuery "syntax" is JavaScript, but the problem is .value is a DOM element property not a jQuery object property.
0

This should cover everything:

$(document).ready(function() {

  var u = $('#username'),
      s = $('#server'),
      p = $('#password');

  function doYourAjaxCall() {
    $.post("connect.php", { 
      s: s.val(), 
      u: u.val(), 
      p: p.val() 
    },
    function(data) {
      $('#hol').html('checking connection...' + u.val() + " " + p.val() + " " + s.val() + " " + data);
    });
  }

  u.keyup(doYourAjaxCall);
  s.keyup(doYourAjaxCall);
  p.keyup(doYourAjaxCall);

});

As always, I'd recommend reading How Good C# Habits can Encourage Bad JavaScript Habits

Comments

0

as an alternative you can use serialize() method:

Encode a set of form elements as a string for submission

$('#server').keyup(function () {
         $.post("connect.php", $('input[type=text]').serialize(),
             function(data) {
             $('#hol').html('checking connection ' + data);
         }); 
   });

1 Comment

But you've still got u.value, p.value and s.value which was the problem in the original code..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.