3

I am using asp.net MVC.

I have control like

<%= Html.TextBox("username") %>

I want lost focus event to that control.

So code like

$(document).ready(function() {
  $("#username").Attributes.Add("onblur", "alert('losing focus');");
});

but it is not working,

Ultimate goal is to check password & confirm password matches

help me!

4 Answers 4

15

It looks like you're trying to use C# code in jQuery?

The easiest way to bind an event to onblur in jQuery is:

$("#username").blur(function() { alert('losing focus'); });

More information on blur() is available at http://docs.jquery.com/Events/blur

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

Comments

1

You can try attach to this event with another way, like this:

$("#username").bind("blur", function(e){
  alert('hello');
});

Comments

1
$(document).ready(function() {
  $("#username").blur(function() {
    alert('byebye focus');
  });
});

http://docs.jquery.com/Events/blur

Comments

0

I believe your jQuery syntax is wrong,

You want to bind an event, "onBlur" that fires the alert, so try

$("#username").blur(function(){
  alert("loosing focus");
});

-- update, looks like some one answered this as I was answering

Comments

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.