Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery trigger blur not working quite right?

Tags:

jquery

I'm confused as to what's going on here...it should be basic, but I can't quite figure it out.

I'm prepoulating a form with some information that needs to be checked against the database. When the form loads I want javascript to run the "blur" events for the two input boxes I need to verify against.

Here's whats happening. When the form loads all the data shows properly, but the trigger("blur") event for both the "username" and "email" inputs does not get triggered. However, if I manually place the cursor into each of the inputs and then press the tab key, or click outside the box, the "blur" event gets triggered.

I need it to also trigger when the page loads to warn the user if the pre-populated data is in conflict with existing data...any idea what I'm doing wrong or how to make this work?

Thanks in advance.

<script type="text/javascript">
    $(document).ready(function(){
        //Username
        $("#username").blur(function(){
            $("#usr_available_msg").html("Checking").addClass("error_msg ui-state-error");
        });

        //email
        $("#email").blur(function(){
            $("#email_available_msg").html("Checking").addClass("error_msg"); 
        });

        //Run validation check on page load
        $("#username").trigger("blur");
        $("#email").trigger("blur");
    });
</script>
like image 963
Ronedog Avatar asked Jan 05 '12 17:01

Ronedog


People also ask

How to bind blur event in jQuery?

$( "#field1" ). blur(function() { alert( "Lose focus from Field1" ); }); Note : In jQuery blur( handler ) method is used to bind an event handler to the "blur" JavaScript event, or trigger that event on an element.

What triggers blur event?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

When would an on blur event occur?

The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event.


2 Answers

Why not take your 'checking' functions out of the blur event for the username and email elements and make them into their own named function or functions? Then call the function on the page load and blur events (for those two elements). Your code is working properly since when the page loads you're triggering a blur on something that never had focus to begin with.

like image 65
j08691 Avatar answered Oct 11 '22 18:10

j08691


This should work as expected.

Try the following code in new file and test it... If it is working fine then the problem is with another block of code on the same page which is causing this issue.

  • Create one simple HTML page
  • Add jQuery script file to it
  • Add one input with id "testinput"
  • Add following script

    $(document).ready(function(){
        $("#testInput").blur(function(){
            alert(1);
        });
    
        $("#testInput").trigger("blur");
    });
    
like image 27
K D Avatar answered Oct 11 '22 16:10

K D