This function extends the native JavaScript
select()
(which
only works
on
input
and
textarea
) so that it can be used to select the content of every DOM element you want.
It's written in Vanilla JavaScript (284 bytes gzipped), so you can use it in combination with or without any library.
Works with
input
/
textarea
:
You can also select child elements if the parent gets a click.
The
img
needs a wrapper element in order to be selectable.
Download
_select.min.js
and include it into your page / script.
var el = document.querySelector('.demo');
el.addEventListener('click', function() {
_select(this);
});
jQuery
var el = $('.demo');
el.on('click', function() {
_select(this);
});
2013 by Tim Pietrusky Reddit