Skip to content
Advertisement

getSelection().focusNode inside a specific id doesn’t work

I have code to bold/unbold scripts via Window.getSelection() I got it from this answer: Bold/unbold selected text using Window.getSelection()

It really works without problems . But when I looked for another code to make the selection inside a div specified with id I found this answer : How to getSelection() within a specific div? When I try to combine the first answer with the second answer I found it doing bold and not unbold My Code :

JavaScript
JavaScript

Like I said when you combined the two an swers Makes bold and ignores unbold

If you delete the first line of the validation condition, if(window.getSelection().baseNode.parentNode.id It will work successfully Bold/Unbold

Advertisement

Answer

that window.getSelection().baseNode.parentNode.id When creating bold

The parent element of the text is obtained and find it Text Therefore, he did not reach the main father element who kissed him So replace the first line with this line and it works successfully Replace

window.getSelection().baseNode.parentNode.id != “editor”

To

window.getSelection().focusNode.parentElement.closest(“#editor”).id != “editor”

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement