Ok I'm using the jQuery Mobile framework and I would like to be able to use the default values in a text or text area as helper text. But on focus clear the text and keep the newly entered value. Also on re-focus (say they touch it again by mistake) not to clear the value again.
I'm including these
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>
The example is not working
Adding this to the page itself: (link to example)
<script type="text/javascript">
$('.default-value').each(function() {
var default_value = this.value;
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function() {
if(this.value == '') {
this.value = default_value;
}
});
});
</script>
Here is the HTML (It's in a form tag as well)
<!-- Address 2 -->
<div data-role="fieldcontain">
<label for="address-2">Address 2</label>
<input type="text" name="address-2" id="address-2" class="default-value" value="Apt #, Suite #, Floor #" />
</div>
I was able to get your code to work using jQuery 1.5 and jQuery Mobile 1.0a3. Did you include jQuery as well as jQuery Mobile? jQuery Mobile requires a version of jQuery to work.
Edit: I should note that I only tested this on the iPhone simulator, and not an actual device. The simulator was running iOS 4.0.1.
<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script><script src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.js" type="text/javascript"></script></head>
<div data-role="fieldcontain">
<label for="address-2">Address 2</label>
<input type="text" name="address-2" id="address-2" class="default-value" value="Apt #, Suite #, Floor #" />
</div>
<script type="text/javascript">
$(document).ready(function() {
$('.default-value').each(function() {
var default_value = this.value;
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function() {
if(this.value == '') {
this.value = default_value;
}
});
});
});
</script>