Skip to content

Instantly share code, notes, and snippets.

@jolij
Forked from anonymous/index.html
Last active August 29, 2015 14:10
Show Gist options
  • Save jolij/a5458bda752ee4420c0d to your computer and use it in GitHub Desktop.
Save jolij/a5458bda752ee4420c0d to your computer and use it in GitHub Desktop.
$.fn.OneClickSelect = function(){
return $(this).on('click',function(){
var range, selection;
if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
} else if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
});
};
// Apply to these elements
$('p, #all-select').OneClickSelect();
function SelectText(element) {
var doc = document,
text = doc.getElementById(element),
range,
selection;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
$(function() {
$('p').click(function () {
SelectText('autoselect');
});
});
<script src="http://code.jquery.com/jquery-latest.js"></script>
<div>
<p>Some cool text!</p>
<br/>
<p>Some cool text!</p>
</div>
<div id="all-select">
<p>Some cool text!</p>
<br/>
<p>Some cool text!</p>
</div>
<script id="jsbin-javascript">
$.fn.OneClickSelect = function(){
return $(this).on('click',function(){
var range, selection;
if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
} else if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
});
};
// Apply to these elements
$('p, #all-select').OneClickSelect();
</script>
<script id="jsbin-source-javascript" type="text/javascript"> $.fn.OneClickSelect = function(){
return $(this).on('click',function(){
var range, selection;
if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
} else if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
});
};
// Apply to these elements
$('p, #all-select').OneClickSelect();</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment