JS how to achieve click on the label text, text in the text box appears _javascript skills

Source: Internet
Author: User

JS Implementation Click on the label text, text appears in the text box, the following 2 methods to achieve a slightly different function, the overall method a more powerful function.

The first way: Random Click any label will appear in the text box, how to click the second tab, the corresponding text will 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" > City level,</div>< Div class= "C" id= "C1" > Provincial, </div><div class= "C" id= "C2" > National
<script>
var x=["city Level", "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>

Effect Chart:

The second way: When you click on the "Cloud Communities" tab, this label appears in the text box, and then click on the SQL tag, the "SQL" tag will replace the "Cloud Community" tab appears in the text box.

<style>.label {cursor:pointer}</style>

<input type= "text" id= "textbox" size = "a" name= "CHAXINMD" />
<span class= "label" >JS special effects </span>
<span class= "label" >C++</span>
< Span class= "label" >SQL</span>
<span class= "label" > cloud-dwelling Community </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>

Effect Chart:

Here again for you to share another JavaScript tricks-----JS Implementation Click the text box to clear the default text inside the form, sometimes when you fill out the form content, the form will appear some default hint text, we do not want to delete one after another, Is there any way to achieve quick removal? Here's the little code I've 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= "Please 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>

Effect Chart:

After clicking the text box

The above is the entire content of this article, I hope to help you learn.

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.