I have a contenteditable div where the user is able to type text, select it and then add a link to it (by clicking a button). They can then remove the link by hovering over the text and clicking another button.
Currently, both buttons (link and unlink) display. Is it possible to have the unlink button hidden and when the user selects some text inside the div, it checks if the text has a link:
- If so, the link button will hide and the unlink button will appear. They can then click the unlink button to remove the link
If not, the link button will stay as it is.
<button type="button" class="center" id="link">Link</button> <button type="button" class="center" id="unlink">Unlink</button> <div style="border: 1px solid;" contenteditable> <script> $('#link').click(function() { var linkURL = prompt("Enter the URL for this link:", "http://"); document.execCommand("CreateLink", false, linkURL); updateInput() }); $('#unlink').click(function() { document.execCommand("UnLink", false, null); updateInput() }); </script>
JsFiddle: https://jsfiddle.net/yw66s03e/