I have content content div with content:
<span style="font-weight: 700">S</span> <span style="font-weight: 100">o</span> <span style="color: red">m</span> <span style="text-decoration: underline">e</span> <span style="background-color: green">T</span> <span style="text-decoration: line-through">e</span> <span style="font-style: italic">x</span> <span style="font-family: Arial">t</span>
And I want to get the style of the element, which is up to the carriage (cursor). For example, if I have a caret between Some and Text , I want to get text-decoration: underline . My code is below (it works, but I get the style of the last element ( font-family: Arial )). How can I fix this using javascript and / or jquery?
Thanks for the help.
$('button').click(function() { var style = $('#Box span').last().attr('style'); $('#Result').text('Result: ' + style); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="Box" contenteditable="true"> <span style="font-weight: 700">S</span><span style="font-weight: 100">o</span><span style="color: red">m</span><span style="text-decoration: underline">e</span><span style="background-color: green">T</span><span style="text-decoration: line-through">e</span><span style="font-style: italic">x</span><span style="font-family: Arial">t</span> </div> <button>Get result</button> <p id="Result"></p>
source share