Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem Customize <input type="search" ...> Jquery Mobile

I want to show the message "search" in the filed with an id of "search" and have it disappear when the user clicks. Here is my code. It doesn't work.

 <script>
 $('#acceuil').live('pagecreate',function(event){
 $('#search').blur(function(){
    if ($('#search').val()=='')
    $('#search').val("search");
});

$('#search').focus(function(){
    if (('#search').val()=='search')
    $('#search').val("");
});
});
</script>



<div data-role="page" id ="acceuil" >
<div data-role="header" data-theme="a" ><h3>TEST</h3></div>
<div data-role="content">
   <div data-role="container" >
    <img src="images/olampromo.gif">
</div>
<div data-role="container">
    <label for="search">Cherchez votre coupon :</label>
    <input type="search" id="search" value="search">        
</div>

</div>
<div data-role="footer" data-theme="a" ><h3>footer</h3></div>
like image 990
Wassim Sboui Avatar asked May 25 '26 04:05

Wassim Sboui


2 Answers

Why not use the HTML5 native placeholder attribute? no JS needed.

<input type="text" id="searchBox" placeholder="search..." />

Live Example:

  • http://jsfiddle.net/phillpafford/TQYvT/1/
like image 54
Phill Pafford Avatar answered May 27 '26 17:05

Phill Pafford


You missed a $ in your JS. Try changing the line:

if (('#search').val()=='search')

to:

if ($('#search').val()=='search')

You could further improve it by making use of this. this will refer to the object that the event was triggered on (i.e. the search element). So, try instead:

if ($(this).val()=='search')

The same logic applies to both the blur and focus handlers.

Here's a jsFiddle demonstrating the solution.

like image 26
Spycho Avatar answered May 27 '26 18:05

Spycho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!