简体   繁体   中英

How to show the placeholder value after clearing the text field in Jquery

I'm setting a text field value to empty like this

$("#navbarSearchField").val('');

now I want to re show the place holder value. I tried triggering a foucusout and a blur but neither worked.

$("#navbarSearchField").trigger("blur"); // and .trigger("focusout");

Check this out

 $("#s").blur(function() { if($(this).val() == '') $(this).val('some text'); }); 
 <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="crossorigin="anonymous"></script> <input type="text" name="s" id="s" placeholder="some text"> 

this fixed it.

focus on another element outside the form

$("#navbarSearchField").val('');
$("#navbarHeader").first().focus();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM