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>
<开发者_C百科input type="search" id="search" value="search">
</div>
</div>
<div data-role="footer" data-theme="a" ><h3>footer</h3></div>
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/
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.
try this:
$("#search").focus(function(srcc) {
var p_val = "Search";
if ($(this).val() == p_val) {
$(this).val("");
}
});
$("#search").blur(function() {
var p_val = "Search";
if ($(this).val() == "") {
$(this).val(p_val);
}
});
$("#search").blur();
精彩评论