Currently, I am utilizing contenteditable on a div and am eager to include a button that will increase the font size of selected text.
By "selected text," I mean any text highlighted by the cursor.
To execute
document.execCommand('fontsize', false, size);
I must be able to determine the font size of the selected text in order to increment it by 1. How can I retrieve the font size of the currently selected text?
I experimented with the following:
var size = document.getSelection().fontSize.toString();
but unfortunately, it did not work as expected. Additionally, I attempted
document.execCommand('increaseFontSize', true, 1)
expecting it to increase the text size by one unit, yet nothing happened - no error message was displayed either.
This is another approach I tried:
var el = document.getSelection();
var size = window.getComputedStyle(el, null).getPropertyValue('font-size');
However, this resulted in the following error:
Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'