JS Select Select Dropdown box value automatically fill in the input box

Source: Internet
Author: User

This function is more practical, many cases we can use, in fact, the code is quite simple, you can also unlimited add any drop-down list box value, selected automatically displayed in the text box.


JavaScript uses ParentNode.nextSibling.value to implement this function, you will actually find that there are two control elements on the Web page, one is select, one is input, use CSS to overwrite the input on the Select, and then use JS to assign the value of the Drop-down box to Input, is actually using input to simulate the function of select, the idea is very novel, also do not know how many people need select can enter text function, the following is the detailed implementation code:

The code is as follows Copy Code

<div style= "position:relative;" >
<span style= "Margin-left:100px;width:18px;overflow:hidden;" >
<select style= "width:118px;margin-left:-100px" onchange= "This.parentnode.nextsibling.value=this.value" >
<option value= "Sky blue" > Azure blue </option>
<option value= Green grass > Green Grass </option>
<option value= "water clear" > Water qingqing </option>
</select></span><input name= "box" style= "width:100px;position:absolute;left:0px;" >


In this example, the CSS implementation input box positioning, JS implementation of the user selected select the value displayed in the input.

  code is as follows copy code


< html>
<title> dropdown box selected values are automatically added to the text box </title>
<body>
<form name= "Form1" >
<select name= menu1 "onchange=" document.form1.get.value=this.options[ This.selectedindex].value ">
<option selected> Please choose </option>
<option value=" Baidu "> Baidu </ option>
<option value= "NetEase" > NetEase </option>
<input type= "text" value= "" name= "get" id= "get" > br> </form>
</body>

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.