1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Enclose keywords in double-elements

Discussion in 'JavaScript' started by Alex100, Aug 31, 2020.

  1. #1
    I would like to kindly ask you to please take five minutes of your time to help me with the code below. My JS experience is very limited and I have been struggling for hours to get this done, without any success. Any help would be very much appreciated.

    I am using a JavaScript that highlights certain keywords on the web.

    For this, the script encloses these keywords within a '<mark>' tag, like this:

    
    <mark>highlighted keyword</mark>
    
    Code (markup):

    The function responsible for this job is this...

    
    key: "WrRn",
    value: function WrRn(node, start, end) {
    
      var elm = !this.opt.element ? 'mark' : this.opt.element,
      startNode = node.splitText(start),
      rtn = startNode.splitText(end - start);
    
      var replacement = document.createElement(elm);
    
      replacement.textContent = startNode.textContent;
      startNode.parentNode.replaceChild(replacement, startNode);
    
      return rtn;
    
    }
    
    Code (markup):

    My question is this: how could I enclose the highlighted keyword in two elements, rather than one? Something like this:

    
    <div><mark>highlighted keyword</mark></div>
    
    Code (markup):

    I will later use the 'setAttribute' instruction to apply some CSS styling to the div element. Applying this directly to the 'mark' element would not be helpful, so that's why it's important that I add this second element too.

    If you could please be highly specific, that would be great.

    I use this script on a daily basis, and this improvement would make a huge difference for me.

    Thank you,

    Alex
     
    Last edited: Aug 31, 2020
    Alex100, Aug 31, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Simply create another element, append your existing child (mark) to it, then replaceChild the new element in place of the mark.

    
      var
    		rep_outer = document.createElement('div'),
    		rep_inner = rep_outer.appendChild(document.createElement(elm));
    		
      rep_inner.textContent = startNode.textContent;
      startNode.parentNode.replaceChild(rep_outer, startNode);
    
    Code (markup):
    Also don't forget that appendChild / insertBefore / replaceChild all return the element that was inserted. You can leverage that to simplify your code in some places.
     
    deathshadow, Aug 31, 2020 IP
  3. Alex100

    Alex100 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Awesome, you are my hero, and I definitely owe you one! Works great.

    Thank you, Sir!

    Alex
     
    Alex100, Sep 1, 2020 IP