How does js achieve click label text, text appears in the text box, js text box

Source: Internet
Author: User

How does js achieve click label text, text appears in the text box, js text box

Js implements click label text and text appears in the text box. The functions of the following two methods are slightly different. In general, method 1 is more powerful.

Method 1: clicking any label at Will will appear in the text box. How to click the label for the second time will the corresponding text disappear in the text box.

<Style>. c {width: 40px; height: 25px; line-height: 25px; text-align: left; margin: 2px; float: left; font-size: 12px; cursor: pointer} </style> <input id = "I" type = "text"> <div class = "c" id = "c0"> municipal, </div> <div class = "c" id = "c1"> provincial, </div> <div class = "c" id = "c2"> National <script> var x = ["municipal", "Provincial", "national"]; var y = []; function $ (id) {return document. getElementById (id)}; for (var I = 0, m = x. length; I <m; I ++) {$ ("c" + I ). onclick = (function (I) {return function () {var s = y. join (","). indexOf (x [I]); if (s> = 0) {for (var r in y) {if (y [r] = x [I]) {y. splice (r, 1) }}} else {y. push (x [I])} $ ("I "). value = y. join ("") ;}}) (I)} </script>

:

Method 2: When you click the "" tag, the tag appears in the text box, and then click the SQL tag, the "SQL" label will replace the "" label in the text box.

<Style>. label {cursor: pointer} </style> <input type = "text" id = "textbox" size = "30" name = "chaxinmd"/> <span class = "label"> JS Special Effects </span> <span class = "label"> C ++ </span> <span class = "label"> SQL </span> <span class = "label"> helper house </span> <script type = "text/javascript"> spans = document. getElementsByTagName ("span"); for (I = 0; I <spans. length; I ++) {if (spans [I]. className = "label") {spans [I]. onclick = function () {document. getElementById ('textbox '). value = this. innerHTML ;}}</script>

:

Here we will share with you another tips on Javascript -----In JS, click the text box to clear the default text in the form,Sometimes when you fill in the Form Content, some default text prompts will appear in the form, and we do not want to delete them one by one. Is there any way to quickly clear them? Below is the small code I have prepared for you:

<Form action = "" method = "get" name = "so_box" id = "so_box"> <input name = "so_name" type = "text" id = "so_name" onFocus = "if (value = defaultValue) {value = '';}" onBlur = "if (! Value) {value = defaultValue ;} "value =" Enter the keyword "> <input type =" submit "name =" Submit "value =" submit "onFocus =" if (so_box.so_name.value = so_box.so_name.defaultValue) {so_box.so_name.value = '';}"> </form>

:

Click the text box

The above is all the content of this article, hoping to help you learn.

Related Article

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.