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
https://forums.phpfreaks.com/topic/244451-converting-to-a-function/
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);
   
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.