Selecting Text On Focus Using Jquery Not Working In Iphone Ipad Browsers
We are developing Web-App for Mobile browsers, we would like text of any input box to get selected whenever input-box get focus. Below answer fixes the issue for Desktop chrome/saf
Solution 1:
Answer found here don't know how to mark this question as duplicate. Programmatically selecting text in an input field on iOS devices (mobile Safari)
My fix look like this:-
<scripttype="text/javascript">
$('div,data-role=page').live('pageshow', function (event) {
jQuery.validator.unobtrusive.parse(".ui-page-active form");
$("input[type='text'], textarea, input[type='password'], input[type='number']").live('mouseup', function (e) { e.preventDefault(); });
$("input[type='text'], textarea, input[type='password'], input[type='number']").live('focus', function () {this.setSelectionRange(0, 9999); });
});
</script>
Solution 2:
Maybe try this:
vmouseupNormalized event for handling touchend or mouseup events
JS:
$("#souper_fancy").focus(function() {
$(this).select();
});
$("#souper_fancy").vmouseup(function(e){
e.preventDefault();
});
Solution 3:
You can use document.execCommand('selectAll');
. However if the user scrolls the page, you will get the copy/paste menu showing.
This is what I use:
functiontrySelect(el, evenInactive, select_ios) {
var select = function() {
try {
if (mojo.isTouch && select_ios && core.iosDevice && mojo.isInput(el) && document.execCommand) {
document.execCommand('selectAll');
} else {
el.select();
}
} catch (e) {
}
};
if (el && el.select && !el.disabled && (!el.readOnly || el.selectReadOnly) && mojo.isInput(el)) {
if (evenInactive || mojo.activeElement() === el) {
if (mojo.isTouch && core.webkitVer) { // http://code.google.com/p/chromium/issues/detail?id=32865setTimeout(select, 0);
} else {
select();
}
}
}
}
That references some internal functions:
- mojo.isTouch - true for a touch device
- core.iosDevice - true for an iOS device
- mojo.isInput - tests for an input element
- mojo.activeElement() - document.activeElement
edit: document.execCommand('selectAll');
should not be used and el.setSelectionRange(0, el.value.length);
used instead. That seems to work fine on iOS5... It might not work on iOS4 (I don't have an iOS4 device to test on).
Solution 4:
This one works for me.
$("input[type='text'],select,textarea").focus(function () {
$(this).addClass('text-focus');
});
$("input[type='text'],select,textarea").blur(function () {
$(this).removeClass('text-focus');
});
.text-focus
{
border: 1px solid #ea9a00;
}
Post a Comment for "Selecting Text On Focus Using Jquery Not Working In Iphone Ipad Browsers"