Clearing the textbox values onclick and displaying onblur

前端 未结 3 1371
生来不讨喜
生来不讨喜 2021-02-15 11:33

Is there a way to clear the default textbox value onclick on textbox and display onblur of multiple textboxes on form page?

相关标签:
3条回答
  • 2021-02-15 11:57

    function Clear1(str)
    {    
       document.getElementById(str).value= "";
    }
    
    function Clear2(str2)
    {    
    var aa1=document.getElementById(str2);
    	if (aa1.value==""){  
        document.getElementById(str2).style.backgroundColor = "#ffcccc"; 
    	}else{
        document.getElementById(str2).style.backgroundColor = "#ffffff";   
      }
    }
    <input type="text" value="test1" onClick="Clear1(this.id);" id="textbox1" onblur="Clear2(this.id);">
    <input type="text" value="test2" onClick="Clear1(this.id);" id="textbox2" onblur="Clear2(this.id);">
    <input type="text" value="test3" onClick="Clear1(this.id);" id="textbox3" onblur="Clear2(this.id);">
    <input type="text" value="test4" onClick="Clear1(this.id);" id="textbox4" onblur="Clear2(this.id);">
    
    
    
    
        

    https://jsfiddle.net/warunamanjula/qy0hvmyq/1/

    0 讨论(0)
  • 2021-02-15 12:02

    One Line solution

      <input type="text" value="" onClick="this.value='';" id="textbox1">
    

    or

     <input type="text" value="" onClick="this.value=='Initial Text'?this.value='':this.value;" id="textbox1">
    
    0 讨论(0)
  • 2021-02-15 12:03

    HTML:

    <input type="text" value="" onClick="Clear();" id="textbox1>
    <input type="text" value="" onClick="Clear();" id="textbox2>
    <input type="text" value="" onClick="Clear();" id="textbox3>
    <input type="text" value="" onClick="Clear();" id="textbox4>
    

    Javascript :

    function Clear()
    {    
       document.getElementById("textbox1").value= "";
       document.getElementById("textbox2").value= "";
       document.getElementById("textbox3").value= "";
       document.getElementById("textbox4").value= "";
    }
    

    Your question was a little vague to me, but the above will clear all the textboxes when one is clicked. Hopefully this helps you.

    0 讨论(0)
提交回复
热议问题