Selecting text on focus using jQuery not working in Safari and Chrome

user140550 picture user140550 · Aug 13, 2009 · Viewed 78.9k times · Source

I have the following jQuery code (similar to this question) that works in Firefox and IE, but fails (no errors, just doesn't work) in Chrome and Safari. Any ideas for a workaround?

$("#souper_fancy").focus(function() { $(this).select() });

Answer

keithnorm picture keithnorm · Aug 13, 2009

It's the onmouseup event that is causing the selection to get unselected, so you just need to add:

$("#souper_fancy").mouseup(function(e){
    e.preventDefault();
});