开发者

Error in html file with Javascript onBlur event

开发者 https://www.devze.com 2023-03-30 12:30 出处:网络
I\'m reading a tutorial of Javascript, I\'m making a html file with a javascript function with a box, the fact is that the alert does not show what goes into the text field, what am I doing wrong ?, t

I'm reading a tutorial of Javascript, I'm making a html file with a javascript function with a box, the fact is that the alert does not show what goes into the text field, what am I doing wrong ?, this is my code. I'm not entirely clear, how it handles the event onBlur, someone explain more about it? as in the tutorial the only explanation they give is "Losing the focus control," I don't know what they mean by the word focus, or how it is handled the text box, without enter botton.

<html开发者_JS百科>
    <head>
        <script>
            function show ()
            {
            var age= parseInt (cage.value);
                if (age<=18)
            alert("access denied");
                else
            alert("Welcome");
            }
        </script>
        <title> New Page I </title>
    </head> 

    <body>  
        Age: 
        <input type="text" id "cage" name="cage" size="10"
    onBlur=show();> 

    </body>
</html>


The onBlur event fires when the current control loses focus. When a control has focus, meaning that it's currently "selected", things that can cause it to lose focus include clicking on another control or pressing the tab key.

The reason your method does not work properly is because it has a small error. Try this:

<html> 
    <head> 
        <script> 
            function show(el) 
            { 
              var age = parseInt(el.value); 
              if (age<=18) 
                alert("access denied"); 
              else 
                alert("Welcome"); 
            } 
        </script> 
        <title> New Page I </title> 
    </head>  

    <body>   
        Age:  
        <input type="text" id="cage" name="cage" size="10" onBlur="show(this);" />  
     </body> 
</html> 


You have to use () after function name and look up the element first and then retrieve the value from it.. here is sample code.. (should work, not tested)

 function show(){
       var age = document.getElementById('cage').value;
       if (parseInt(age, 10)<=18)
            alert("access denied");
        else
            alert("Welcome");
    }


the onBlur event is fired when you click on anything other than the input box (e.g. clicks another one, or click on a link to something). Basically, if you can't type in the input field, it does not have focus.

As for why it's not working, see @Teja Kantamneni 's answer, which should make it work.


You can getting an error because you are missing the parentheses() after the function declaration. Even if you have a function with no parameters must include the parentheses () after the function name.

Focus is when a form element receives control, so you can type in it or change options of a dropdown. Like onBlur there is also an onFocus event.

Focus is generally set on page load. so when the page is loaded so user does not have to go and click on the first field they need to fill in.

In this example you can have:

Javascript 

function setFocus(){
   document.getElementById('cage').focus();
}

HTML

<body onLoad="setFocus();">  
0

精彩评论

暂无评论...
验证码 换一张
取 消