Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6171317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:11:31+00:00 2026-05-23T23:11:31+00:00

Can someone explain why this code works: $(‘#FonykerUsernameRegister’).blur(function(){ if($(this).val().length > 2) { $.ajax({ url:

  • 0

Can someone explain why this code works:

$('#FonykerUsernameRegister').blur(function(){
            if($(this).val().length > 2) {
                $.ajax({
                    url: '<?php echo $html->url('/fonykers/validate_username',true); ?>' + '/' + $(this).val(),
                    dataType: 'json',
                    type: 'POST',
                    success: function(response) {
                        if(!response.ok) {
                            $('#FonykerUsernameRegister').addClass('error');
                            error.html(response.msg);
                            error.fadeIn();
                        } else {
                            if($('#FonykerUsernameRegister').is('.error')) {
                                $('#FonykerUsernameRegister').removeClass('error');
                            }
                            $('#FonykerUsernameRegister').addClass('ok');
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(xhr.statusText);
                        alert(thrownError);
                    } 
                });        
            } else {
               error.html('Username must have at least 3 characters');
               error.fadeIn();
               $('#FonykerUsernameRegister').addClass('error');
            }
        });

As opposed to this one:

$('#FonykerUsernameRegister').blur(function(){
            if($(this).val().length > 2) {
                $.ajax({
                    url: '<?php echo $html->url('/fonykers/validate_username',true); ?>' + '/' + $(this).val(),
                    dataType: 'json',
                    type: 'POST',
                    success: function(response) {
                        if(!response.ok) {
                            $(this).addClass('error');
                            error.html(response.msg);
                            error.fadeIn();
                        } else {
                            if($(this).is('.error')) {
                                $(this).removeClass('error');
                            }
                            $(this).addClass('ok');
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert(xhr.statusText);
                        alert(thrownError);
                    } 
                });        
            } else {
               error.html('Username must have at least 3 characters');
               error.fadeIn();
               $(this).addClass('error');
            }
        });

I’m assuming the second one is a bit more optimized so I’d rather use that way if possible, but it just isn’t setting the classes on the elements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:11:32+00:00Added an answer on May 23, 2026 at 11:11 pm

    The first code (working code) refers to the element by id: the jQuery $ function is basically a replacement for document.getElementById.

    In the second code, when you try to reference the element using this, the scope is such that this will likely refer to the request object or window. The solution, if you want to use the this object with the scope you desire, is to use the proxy method to bind the handler function, or grab the target element as a variable to use in a closure.

    Of the two methods, using proxy is the the least likely to end up causing a circular reference. The code you’ve given here is safe, but you really have to watch yourself when you use element references in closures if the target element will be removed from DOM at some point – you’d then potentially have a situation where the browser’s garbage collector cannot free the resources related to the element because the closure is holding open a reference pointer.

    All proxy does is to create a closure with the specified scope, see the docs here.

    For further reading on scope, check out this MDC document and this MDC scope “cheat sheet”

    Closure:

    $('#FonykerUsernameRegister').blur(function(){
        var target = $(this);
        if($(this).val().length > 2) {
            $.ajax({
                url: '<?php echo $html->url('/fonykers/validate_username',true); ?>' + '/' + $(this).val(),
                dataType: 'json',
                type: 'POST',
                success: function(response) {
                    if(!response.ok) {
                        target.addClass('error');
                        error.html(response.msg);
                        error.fadeIn();
                    } else {
                        if(target.is('.error')) {
                            target.removeClass('error');
                        }
                        target.addClass('ok');
                    }
                },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.statusText);
                    alert(thrownError);
                    alert(target);
                } 
            });        
        } else {
           error.html('Username must have at least 3 characters');
           error.fadeIn();
           $(this).addClass('error');
        }
    });
    

    Proxy

    $('#FonykerUsernameRegister').blur(function(){
        if($(this).val().length > 2) {
            $.ajax({
                url: '<?php echo $html->url('/fonykers/validate_username',true); ?>' + '/' + $(this).val(),
                dataType: 'json',
                type: 'POST',
                success: $.proxy(function(response) {
                    if(!response.ok) {
                        $(this).addClass('error');
                        error.html(response.msg);
                        error.fadeIn();
                    } else {
                        if($(this).is('.error')) {
                            this.removeClass('error');
                        }
                        $(this).addClass('ok');
                    }
                }, this),
                error:$.proxy(function (xhr, ajaxOptions, thrownError){
                    alert(xhr.statusText);
                    alert(thrownError);
                    alert(this);
                }, this) 
            });        
        } else {
           error.html('Username must have at least 3 characters');
           error.fadeIn();
           $(this).addClass('error');
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)
Can someone please explain in plain language how this code works to give a
Can someone please explain this piece of code? struct Class { boost::function<void()> member; };
Can someone explain to me why this code prints 14? I was just asked
Can someone write some sample code to explain this concept? I know what a
Can someone please explain this to me? I have the following code: <form action=<?php
Can someone dumb it down and explain, how this code fragment from a previous
I'm really confused as to why this operation works. Can someone explain it? $test1
Can someone please explain why this JavaScript code outputs zero instead of one? Also,
Can someone explain me why this code give me back always only one bluetooth

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.