Select text in span tag

What will be the approach to selecting text in the span tag when considering browser compatibility? Example:

jQuery().html('<p>Hello world <span>lorem ipsum</span> my good friend!');

I want the part to lorem ipsumbe selected with the cursor.

I have this code that selects text:

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);
    }
}
+4
source share
2 answers

do you mean this?

 var i = 0; 
function SelectText(element) {
    var doc = document
        , text = doc.querySelectorAll(element)
        , range, selection
    ;
 
    if (doc.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(text[i]);
        range.select();
    } else if (window.getSelection) {
        selection = window.getSelection();        
        range = document.createRange();
        range.selectNodeContents(text[i]);
        selection.removeAllRanges();
        selection.addRange(range);
    }
  i++;
   if ( i === text.length ) i = 0;
}

document.onclick = function(e) {    
    if (e.target.className === 'click') {
        SelectText('span');
    }
};
<div>Hello world <span>lorem ipsum</span> my good friend!</div>
<div>Hello world <span>lorem ipsum</span> my good friend!</div>
<p class="click">Click me!</p>
Run codeHide result

if you just need to select one tag element, you can use querySelector instead of querySelectorAll


here is an example with .html ()

$(function() {
   $('body').html('<p>Hello world <span>lorem ipsum</span> my good friend!');
  })

function SelectText(element) {
    var doc = document
        , text = doc.querySelector(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);
    }
  
}

window.onload = function() {    
 
        SelectText('span');
    
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Run codeHide result
+2
source

HTML code

<input type="text" name="firstName" value="Enter your name..." />

Js code

//SELECT TEXT RANGE
$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};
$('input[name=firstName]').focus().selectRange(5,10);

Click here for a permanent link to jsfiddle

//SELECT TEXT RANGE
$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};
$('input[name=firstName]').focus().selectRange(5,10);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="firstName" value="Enter your name..." />
Run codeHide result
0
source

Source: https://habr.com/ru/post/1629632/


All Articles