How can I delete any value in input when clicked? I have two functions fillField(input)
function clearField(input)
and which I want to use in all inputs. My code is:
<script> // identify form elements: var search_code = document.getElementById('search_code'); var insert_code = document.getElementById('insert_code'); var result = document.getElementById('result'); var button = document.getElementById('button'); // respond to button click button.onclick = function validate() { // show verification result: if(search_code.value == insert_code.value) { result.textContent = 'code is ok'; result.className = "ok"; } else { result.textContent = 'code is not ok'; result.className = "not-ok"; } // clear input when wrong: if (search_code.value !== insert_code.value) { insert_code.value = ''; } return false; }; function fillField(input) { if(input.value == "any value") input.value=""; }; function clearField(input) { if(input.value == "") input.value= "any value"; }; insert_code.oninput = function () { result.textContent = ''; // clear result; }; </script>
<form> <input type="text" name="search_code" onblur="fillField(this,'insert code');" onfocus="clearField(this,'insert code');" id="search_code" value=""/><br/> <input type="" name="insert_code" onblur="fillField(this,'scan code');" onfocus="clearField(this,'scan code');" id="insert_code" placeholder="scand code"value=""/><br/><br/> <input type="submit" id="button" name="button" value="validation" /> </form>
Thank you very much!
I solved this the problem. For those in need, the code is:
<script> // identify form elements: var search_code = document.getElementById('search_code'); var insert_code = document.getElementById('insert_code'); var result = document.getElementById('result'); var button = document.getElementById('button'); // respond to button click button.onclick = function validate() { // show verification result: if(search_code.value == insert_code.value) { result.textContent = 'cod gasit'; result.className = "ok"; } else { result.textContent = 'codul nu este corect'; result.className = "not-ok"; } // clear input when wrong: if (search_code.value !== insert_code.value) { insert_code.value = ''; } return false; }; //clear text when click on input function clearField(input) { input.value = ""; }; </script>
<form> <input type="text" name="search_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value=""/><br/> <input type="" name="insert_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="introdu codul" autocomplete="off" value=""/><br/><br/> <input type="submit" id="button" name="button" value="verifica COD" /> </form> <div id="result"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.