<input type= "text"/> The default value of attribute value when not entered--js learning path

Source: Internet
Author: User

In Baidu Ife brush problem is own a mistake caused me to <input type= "text"/> Learning.

Paste the code First:

<! DOCTYPE html> for= "Weather_input" > Please enter Beijing today air quality: <input id= "Weather_input" type= "text"/></label> <button id= "confirm _button "> Confirmation </button> <p> The value you entered is: <span id=" Value_show "> Not yet entered </span></p><script Type= "Text/javascript" >window.onload=function(){  varButton=document.getelementbyid ("Confirm_button"); varSpan=document.getelementbyid ("Value_show"); varInput=document.getelementbyid ("Weather_input"). Value; Button.onclick=function() {span.innerhtml=input;}}</script></body>

This code is syntactically correct, but logically problematic: Var Input=document.getelementbyid ("Weather_input"). value; Input in this statement gets the default value of input box. When the Button.onclick is triggered, the value of input cannot be updated immediately, and the program will not be able to display the immediate effect.

This leads to the question that is discussed today: what is the default value of value when the initial value of value is not specified in the <input type= "text"/> and the user is not entered?

null or undefined or ""?

from Var Input=document.getelementbyid ("Weather_input"). Value See, we can see document.getElementById ("Weather_input") This element node object is present, is not empty, and therefore excludes null.

As to whether it is "" or undefined, I have found the answer through practice.

<! DOCTYPE html> for= "Weather_input" > Please enter Beijing today air quality: <input id= "Weather_input" type= "text"/></label> <button id= "confirm _button "> Confirmation </button> <p> The value you entered is: <span id=" Value_show "> Not yet entered </span></p><script Type= "Text/javascript" >window.onload=function(){  varButton=document.getelementbyid ("Confirm_button"); varSpan=document.getelementbyid ("Value_show"); Alert (document.getElementById ("Weather_input"). value===undefined);//Verify that it is equal to undefinedAlert (document.getElementById ("Weather_input"). value=== "");//Verify that equals ""}}</script></body>

From the above code, I see the program run the result is: The first pop-up box is displayed as false, the second pop-up box is displayed as true.

The conclusion is obvious: the default value of value is "" when the initial value of value is not specified in the <input type= "text"/> and the user is not entered at the same time.

<input type= "text"/> The default value of the property when not entered--js learning path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.