Jump to content

Converting to a function?!


rockinaway

Recommended Posts

Right, I have the following set of code to check form entires:

 

$('#username').keyup(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateUsername.html('<img src="templates/img/loader.gif" height="16" width="16" /> checking availability...');
      
      this.timer = setTimeout(function () {
        $.ajax({
          url: 'register.php',
          data: 'action=check_username&username=' + t.value,
          type: 'post',
          dataType: 'json',
          success: function (j) {
            
            
            if (j.ok === true) 
            {
            	$("#register").removeAttr("disabled");
            	field_success(validateUsername);
            	validateUsername.html(j.msg);
            }
            else 
            {
            	$("#register").attr("disabled", "disabled");
            	field_error(validateUsername);
            	validateUsername.html(j.msg);
            }
          }
        });
      }, 200);
      
      this.lastValue = this.value;
    }
   });

 

This works perfectly fine when I'm trying to input data etc. However, I am trying to convert this into a separate function that can be called again. So something like this:

 

function check_user(this)
{
var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateUsername.html('<img src="templates/img/loader.gif" height="16" width="16" /> checking availability...');
      
      this.timer = setTimeout(function () {
        $.ajax({
          url: 'register.php',
          data: 'action=check_username&username=' + t.value,
          type: 'post',
          dataType: 'json',
          success: function (j) {
            
            
            if (j.ok === true) 
            {
            	$("#register").removeAttr("disabled");
            	field_success(validateUsername);
            	validateUsername.html(j.msg);
            }
            else 
            {
            	$("#register").attr("disabled", "disabled");
            	field_error(validateUsername);
            	validateUsername.html(j.msg);
            }
          }
        });
      }, 200);
      
      this.lastValue = this.value;
    }


}

$('#username').keyup(function () {
  	 check_user(this);
});

 

However, this doesn't work AT ALL. What am I doing wrong?

Link to comment
Share on other sites

Still not working :( .. I have the following:

 

function check_user()
{
var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateUsername.html('<img src="templates/img/loader.gif" height="16" width="16" /> checking availability...');
      
      this.timer = setTimeout(function () {
        $.ajax({
          url: 'register.php',
          data: 'action=check_username&username=' + t.value,
          type: 'post',
          dataType: 'json',
          success: function (j) {
            
            
            if (j.ok === true) 
            {
            	$("#register").removeAttr("disabled");
            	field_success(validateUsername);
            	validateUsername.html(j.msg);
            }
            else 
            {
            	$("#register").attr("disabled", "disabled");
            	field_error(validateUsername);
            	validateUsername.html(j.msg);
            }
          }
        });
      }, 200);
      
      this.lastValue = this.value;
    }


}

$(document).ready(function () {

  var validateUsername = $('#user_error');
  
  $("#register").attr("disabled", "disabled");
  
  $('#username').keyup(check_user);
   
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.