0

I have this code but it gave me only the last selected line!

$('#getLine').click(function(){
    console.clear();
    $('div').css('color','');
    
    document.execCommand('formatblock', false, 'div');
    var line=window.getSelection().focusNode.parentNode;
    $(line).css('color','red')
    console.log(line);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="editable" contenteditable="TRUE">I am a line<div>I am a new line</div><div>I am the last line</div></div>
<br/>
<input id="getLine" value="Get Line" type="button">

How to get all selected lines even if it was partially selected ? I saw this solution but I don't want selected text only I want to wrap HTML of this lines to change their styles together.

Community
  • 1
  • 1
n.y
  • 3,343
  • 3
  • 35
  • 54
  • FYI there is very little jQuery in that answer. – epascarello Feb 14 '17 at 12:54
  • I saw this solution but I don't want selected text only I want to wrap HTML of this lines to change their styles together. – n.y Feb 14 '17 at 13:15
  • For example, suppose I need to change the selected lines "Line-height". – n.y Feb 14 '17 at 13:19
  • So you changed your question to not get the selection but act on it? Did you read the documentation for [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) – epascarello Feb 14 '17 at 13:22
  • I've changed my question to be more understandable.Yes, I've read it. I asked this question because I had a problem that I can not solve it by reading this documentation. – n.y Feb 14 '17 at 13:32
  • OK I found an alternative and don't need to answer of this question. Thank you! – n.y Feb 14 '17 at 13:55

0 Answers0