3

My goal

I have a text in a HTML page, and I want to be able to use two different search boxes on it, using mark.js. The first search box should highlight matching words in a color, and the second box should highlight matching results in a different color.

What I have tried

I have the following code :

CSS :

mark {
  padding: 0;
  background-color:yellow;
}

jQuery :

$(function() {
  var mark = function() {
    // Read the keyword
    var keyword = $("input[name='keyword']").val();
    // Remove previous marked elements and mark
    // the new keyword inside the context
    $(".context").unmark({
      done: function() {
        $(".context").mark(keyword);
      }
    });
  };
  $("input[name='keyword']").on("input", mark);
});

HTML :

<input type="text" name="keyword">
<div class="context">
The fox went over the fence
</div>

Working JSFiddle : JSFiddle.

My problem is that I don't know how to add a second search box without duplicating the JavaScript code ; and when I do, the search in the second search box erases the highlight from the search in the first search box.

I have found the following thread that seems to solve my problem (I had to delete this link because I do not have enough rep to post more than two links), but I get a 404 Error when I try to access the JSFiddles.

How can I add a second search box that allow me to have results of the first search box in a color and the result of the second search box in another color, without having one search erasing the other ?

EDIT

From the answer I got, I believe I did not ask cleary my question. So here is a "half working" JSFiddle, where you can see the two search boxes I need. If I search 'fox' in the first search box and then 'fence' in the second ; 'fox' is not highlighted anymore, but 'fence' is. I would like both to stay highlighted, with different colors. I really don't have a clue how to do this.

Luci
  • 475
  • 1
  • 7
  • 17
  • Link that I had to delete because I don't have enough rep : https://github.com/julmot/mark.js/issues/8 – Luci Apr 28 '17 at 13:13

2 Answers2

8

My previous answer was for two boxes and one input, but this answer is one box and two inputs, as you requires. I don't remove the previous answer if someone needs that solution

This new solution requires a classname, I called .secondary. See the code:

https://jsfiddle.net/1at87fnu/55/

$(function() {
  var mark = function() {
    // Read the keyword
    var keyword = $("input[name='keyword']").val();
    var keyword2 = $("input[name='keyword2']").val();
    // Remove previous marked elements and mark
    // the new keyword inside the context
    $(".context").unmark({
      done: function() {
        $(".context").mark(keyword).mark(keyword2, {className: 'secondary'});
      }
    });
  };
  $("input[name^='keyword']").on("input", mark);
});

And the CSS

mark {
  padding: 0;
  background-color:yellow;
}
mark.secondary {
  padding: 0;
  background-color: orange;
}

You can see on the javascript that I called twice to mark() function, and on the second call I add a className. You can see more options for mark.js here:

https://markjs.io/#mark

This is a screenshot with the final result:

two inputs and one box

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • You are welcome! Please, come back when you have enough reputation for upvote ;) My upvote for your question to give some extra rep. See you! – Marcos Pérez Gude May 02 '17 at 13:03
2

Just add it on your selector:

https://jsfiddle.net/1at87fnu/49/

  $(function() {
    var mark = function() {
      // Read the keyword
      var keyword = $("input[name='keyword']").val();
      // Remove previous marked elements and mark
      // the new keyword inside the context
      $(".context, .context2").unmark({
        done: function() {
          $(".context, .context2").mark(keyword);
        }
      });
    };
    $("input[name='keyword']").on("input", mark);
  });

html

<input type="text" name="keyword">
<div class="context">
    The fox went over the fence
</div>
<div class="context2">
    The fox went over the fence
</div>

Thanks to jQuery recursive selectors, you can add all selectors you want by comma separated:

$('.one, #two, three[disabled]')

And so on.

UPDATE

With CSS you can apply different colors between boxes. Just target the box you are using, like this CSS:

https://jsfiddle.net/1at87fnu/50/

mark {
  padding: 0;
  background-color:yellow;
}
.context2 mark {
  padding: 0;
  background-color: orange;
}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
  • Maybe I misspoke, but from what I see on this JSFiddle this is not at all what I am trying to do. What I need is two **search boxes** only **one text** and both results should highlight in two colors **in the same text**. For example, I would search 'fox' in one search box, 'fence' in another ; and both 'fox' and 'fence' would highlight in the same sentence but in different colors. – Luci Apr 28 '17 at 13:06
  • I just edited my question to illustrate more precisely my problem. – Luci Apr 28 '17 at 13:15
  • Ok, I misunderstood the topic. Sorry. – Marcos Pérez Gude Apr 28 '17 at 13:34
  • I had an idea, I will put a edition on this answer in the following hours (right now I'm leaving work so I can't) – Marcos Pérez Gude Apr 28 '17 at 13:36
  • Are there still questions or is this solved now? If so, please accept this answer. – dude May 01 '17 at 17:12
  • As I stated in my previous comments, this answer doesn't fit my question. What I wanted was : two search boxes, one text, one color in the text for each box. What this answer gives : one search box, two texts, one color in each text. I came up with a solution for having the two search boxes but still don't know how to have two colors (see my edit). – Luci May 02 '17 at 07:03
  • See my new answer, it fits with your requirements. :) – Marcos Pérez Gude May 02 '17 at 11:30