I've used this solution to select the text content for a code box using the code tag.
Jason's Answer which is the following:
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() {
$('span').click(function() {
SelectText('selectme');
});
});
This is my code box:
<div class="codebox"><span>{L_CODE}: {L_SELECT_ALL_CODE}</span><div id="selectme"><code></code></div></div>
The problem is that when there are multiple code boxes in the same page, only the first one is selected because of the ID being the same. How can I use a dynamic way so that when the users clicks to select the desired text, the clicked container will be selected regardless of how many boxes are present?