Created
June 30, 2010 20:59
-
-
Save mscottford/459214 to your computer and use it in GitHub Desktop.
jQuery plugin that protects the selection of textareas when jQuery UI buttons are clicked.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang='en'> | |
<head> | |
<script src="js/jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script> | |
<script src="js/jquery-ui-1.8.2.custom.min.js" type="text/javascript" charset="utf-8"></script> | |
<link rel="stylesheet" href="css/black-tie/jquery-ui-1.8.2.custom.css" type="text/css" media="screen" title="no title" charset="utf-8"> | |
<style type="text/css" media="screen"> | |
html { | |
font-size: 11px; | |
font-family: arial; | |
} | |
#toolbar { | |
padding: 0.5em; | |
margin-bottom: 1.0em; | |
} | |
#text_area_editor textarea { | |
height: 10em; | |
width: 50em; | |
font-size: 1.12em; | |
font-family: arial; | |
} | |
#text_area_editor div { | |
padding: 0.5em; | |
margin-bottom: 0.25em; | |
width: 50em; | |
} | |
#error { | |
font-size: 2em; | |
font-weight: bold; | |
color: white; | |
background-color: red; | |
padding: 0.5em; | |
} | |
</style> | |
<script type="text/javascript" charset="utf-8"> | |
jQuery.fn.protectSelection = function(selectorsAndEvents) { | |
return this.each(function() { | |
function getSelection(element) { | |
result = null; | |
if (element.selectionStart) { | |
result = { | |
start: element.selectionStart, | |
end: element.selectionEnd, | |
range: null | |
}; | |
} else if (document.selection) { | |
result = { | |
start: null, | |
end: null, | |
range: document.selection.createRange() | |
}; | |
} | |
return result; | |
} | |
function setSelection(element, selection) { | |
element.focus(); | |
if (selection.start) { | |
element.selectionStart = selection.start; | |
element.selectionEnd = selection.end; | |
} else if (selection.range) { | |
selection.range.select(); | |
} | |
} | |
function saveSelection(element) { | |
if (element) { | |
$(element).data('protected_selection', | |
getSelection(element)); | |
} | |
} | |
function restoreSelection(element) { | |
if (element) { | |
savedSelection = $(element).data('protected_selection'); | |
if (savedSelection) { | |
setSelection(element, savedSelection); | |
$(element).data('selection', null); | |
} | |
} | |
} | |
element = this; | |
$(this).bind('focusout', function() { | |
saveSelection(element); | |
}); | |
$('body').delegate('.ui-button', 'mouseup', function() { | |
restoreSelection(element); | |
}); | |
$('body').delegate('input', 'click', function(event) { | |
$(event.target).siblings("label[for='" + event.target.id + "']").each(function() { | |
if ($(this).hasClass('ui-button')) { | |
restoreSelection(element); | |
} | |
}); | |
}); | |
}); | |
}; | |
</script> | |
<script type="text/javascript" charset="utf-8"> | |
$(document).ready(function() { | |
$('#error').hide(); | |
$('#anchors a').button(); | |
$('#radio_buttons').buttonset(); | |
$('#checkboxes').buttonset(); | |
$('textarea').protectSelection(); | |
}); | |
</script> | |
</head> | |
<body> | |
<p id='error'> | |
jQuery is not loaded. This demo will not work! | |
</p> | |
<form> | |
<div id='toolbar' class='ui-widget-header ui-corner-all'> | |
<span class='toolbar-label ui-widget'>Radio Buttons:</span> | |
<span id='radio_buttons'> | |
<input type='radio' id='gamma' name='radio_button' checked='true' /><label for='gamma'>Gamma</label> | |
<input type='radio' id='delta' name='radio_button' /><label for='delta'>Delta</label> | |
</span> | |
<span class='toolbar-label ui-widget'>Checkboxes:</span> | |
<span id='checkboxes'> | |
<input type='checkbox' id='epsilon' /><label for='epsilon'>Epsilon</label> | |
<input type='checkbox' id='zeta' /><label for='zeta'>Zeta</label> | |
</span> | |
<span class='toolbar-label ui-widget'>Anchors:</span> | |
<span id='anchors'> | |
<a href='#'>Eta</a> | |
<a href='#'>Theta</a> | |
</span> | |
</div> | |
<div id='text_area_editor' class='ui-widget-container ui-corner-all'> | |
<div class='ui-widget ui-widget-header ui-corner-all'>Text Area</div> | |
<textarea class='ui-widget-content'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea> | |
</div> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Description: | |
// My first crack at a jQuery plug-in that will prevent the | |
// text selection from getting lost when a jQuery UI button | |
// is clicked on. This only works for jQuery UI buttons that | |
// are created with anchors, radios or checkboxes. | |
// Using: | |
// $('textarea').protectSelection(); | |
// Limitations: | |
// This version has only been tested against a textarea element. | |
// I have plans to make sure that this also works for other text | |
// boxes and for regions that are marked as contentEditable. | |
// Dependencies: | |
// jQuery - 1.4.2 | |
// jQuery UI - 1.8.2 | |
// None of the code actually makes use of jQuery UI, but it does look | |
// for markup and styles that were produced by jQuery UI. | |
// License: | |
// Copyright (c) 2010 M. Scott Ford, Corgibytes, LLC | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining | |
// a copy of this software and associated documentation files (the | |
// "Software"), to deal in the Software without restriction, including | |
// without limitation the rights to use, copy, modify, merge, publish, | |
// distribute, sublicense, and/or sell copies of the Software, and to | |
// permit persons to whom the Software is furnished to do so, subject to | |
// the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be | |
// included in all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
jQuery.fn.protectSelection = function(selectorsAndEvents) { | |
return this.each(function() { | |
function getSelection(element) { | |
result = null; | |
if (element.selectionStart) { | |
result = { | |
start: element.selectionStart, | |
end: element.selectionEnd, | |
range: null | |
}; | |
} else if (document.selection) { | |
result = { | |
start: null, | |
end: null, | |
range: document.selection.createRange() | |
}; | |
} | |
return result; | |
} | |
function setSelection(element, selection) { | |
element.focus(); | |
if (selection.start) { | |
element.selectionStart = selection.start; | |
element.selectionEnd = selection.end; | |
} else if (selection.range) { | |
selection.range.select(); | |
} | |
} | |
function saveSelection(element) { | |
if (element) { | |
$(element).data('protected_selection', | |
getSelection(element)); | |
} | |
} | |
function restoreSelection(element) { | |
if (element) { | |
savedSelection = $(element).data('protected_selection'); | |
if (savedSelection) { | |
setSelection(element, savedSelection); | |
$(element).data('selection', null); | |
} | |
} | |
} | |
element = this; | |
$(this).bind('focusout', function() { | |
saveSelection(element); | |
}); | |
$('body').delegate('.ui-button', 'mouseup', function() { | |
restoreSelection(element); | |
}); | |
$('body').delegate('input', 'click', function(event) { | |
$(event.target).siblings("label[for='" + event.target.id + "']").each(function() { | |
if ($(this).hasClass('ui-button')) { | |
restoreSelection(element); | |
} | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment