skip to Main Content

I have a paragraph where some words are highlighted using <mark> tag.
But when the document is ready, some of the words contains <mark>tag inside another <mark> tag.
i.e <mark><mark>MyWord</mark></mark>

Here is the code
`

$(document).ready(function(){
$('mark').each(function () {
if ($(this).next().is('mark')) {
   $(this).next().remove();
}
});
});
</script>`

How will i delete extra mark tags so that MyWord contain single mark tag.i.e
<mark>MyWord</mark>

2

Answers


  1. Perhaps it would be easier to use display: contents CSS on the mark tags, so that although they’re still in the page, the browser ignores them.

    Login or Signup to reply.
  2. $(document).ready(function(){
        let marks = document.querySelectorAll('mark');
        for (let i = 0; i < marks.length; ++i) {
          let mark = marks[i];
          mark.after(...mark.childNodes);
          mark.remove();
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search