Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  81] [ 2]  / answers: 1 / hits: 68428  / 12 Years ago, sun, december 2, 2012, 12:00:00

I am using the event listener structure like below to do some stuff when the input box loses focus.


But it won't work. What event should I listen to, to get the moment the input box loses the cursor inside it (i.e. user clicks outside of it)?


document.getElementById('div inside which input is located')
.addEventListener( 'blur', function(e){
if(event.target.className=='editInput'){
doStuff(event.target);
}
} , false );

More From » html

 Answers
34

The correct event is onBlur. Look at this code:



<!DOCTYPE html>
<html>
<head>
<title>element</title>
<script type=text/javascript>
function message() {
var text = document.getElementById(test).value;
alert(text);
}
</script>
</head>
<body>
<input type=text name=test id=test>
<script type=text/javascript>
document.getElementById(test).onblur=message;
</script>
</body>
</html>


It works and prints the content of the input when it loses focus. Maybe your error is that you attached the event to the div and not to the input?


[#81667] Friday, November 30, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;