4

I have the following placeholder:

 <div class="input-text-container search" style="display:none;">
    <input type="text" id="textSearch" class="input-text-big search-message" placeholder="Search in your list"/>
    <i class="common-sprite cross search"></i>
</div>

The input is displayed when the user clicks on an icon with the class "search.icon-search"

I use the following JS code:

 $('.search.icon-search').on(
        'click',
        function(e) {
            e.preventDefault();
         $('#textSearch').focus()
        });

The problem is that the placeholder disappears on focus. How can I display it even on focus?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Sarit Sara
  • 101
  • 2
  • 13

3 Answers3

9

You don't need any JavaScript or jQuery for this. I have a pure CSS solution. You can use an opacity on focus.

input {opacity: 0; outline: 0; border: 0;}
input:focus {opacity: 1; outline: 0; border: 0;}
span {border: 1px solid #999; display: inline-block;}
<span><input placeholder="This is not visible until you click!" /></span>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
3
input::placeholder{
    opacity: 0;
}

input:focus::placeholder{
    opacity: 1;
}
Yohan Jhaveri
  • 109
  • 1
  • 7
  • 2
    Welcome to SO! Code-only answers are discouraged as they provide no insight into how the problem was solved. Please update your answer to include an explanation of how your code solves the OP's problem :) – Joel Oct 02 '18 at 16:22
  • I think this was great and concise, because it's one of the rare situations where all the CSS properties are self-explanatory. Perhaps the pseudoelements is the most advanced part of the code, so I can see a point in providing a link for readers to read about that. – Elton Lin Jun 29 '22 at 18:52
2

You need to show .input-text-container, but your code works.

Try this:

$('.search.icon-search').on('click',function(e) {
     e.preventDefault();
     $(".input-text-container").show();            
     $('#textSearch').focus()
});

http://jsfiddle.net/3zkpgvj5/

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60