1

I'm looking to create a web page highlighter that will show a few paragraphs on screen but will then allow a person to highlight good lines in green and bad lines in red by highlighting a section of the text and then clicking a button (or typing a number, as is the case in thunderbird). I'd then like to be able to essentially submit this text to a background engine of some kind.

Web Page Text Annotation and Rendering

I've managed to build some jsfiddle code, but couldn't turn on the colour changing on the text and not sure how to get the text to submit to a backend routine.

This snippet isn't fully working but its a step in the right direction...

function myFunction1() {
  $("select#select1").change(function() {
    var color1 = $(this).val()
    $('#selectParagraph1').css('color', color1);
    $('#select1').css('color', color1);
  });
}

function myFunction2() {
  $("select#select2").change(function() {
    var color2 = $(this).val()
    $('#selectParagraph2').css('color', color2);
    $('#select2').css('color', color2);
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table border="1" cellpadding="0">
  <tr>
    <td>
      <div id="selectParagraph1">This is the first paragraph</div>
    </td>
    <td>
      <select name="select1" id="select1" onclick="myFunction1()">
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
        <option selected="selected" value="black">black</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <div id="selectParagraph2">This is the second paragraph </div>
    </td>
    <td>
      <select name="select2" id="select2" onclick="myFunction2()">
        <option value="red">red</option>
        <option selected="selected" value="blue">blue</option>
        <option value="green">green</option>
        <option value="black">black</option>
      </select>
    </td>
  </tr>
</table>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Eamonn Kenny
  • 1,926
  • 18
  • 20
  • I had a look at https://docs.ckeditor.com/ckeditor5/latest/features/highlight.html but I don't think this is what I'd need. It needs to be able to save and submit to another framework I reckon, but I might be wrong. – Eamonn Kenny Apr 18 '18 at 12:58

1 Answers1

1

To highlight text you can wrap it with any element (such as span) and set its background to the wanted color:

$('select').on('change', function() {
  var $this = $(this),
      color = $this.val(),
      textContainer = $this.closest('tr').find('.paragraph');
      
  textContainer.css('background', color);
}).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table border="1" cellpadding="0">
  <tr>
    <td>
      <div class="paragraph">This is the first paragraph</div>
    </td>
    <td>
      <select name="select1" id="select1">
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
        <option selected="selected" value="black">black</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <div class="paragraph">This is the second paragraph </div>
    </td>
    <td>
      <select name="select2" id="select2">
        <option value="red">red</option>
        <option selected="selected" value="blue">blue</option>
        <option value="green">green</option>
        <option value="black">black</option>
      </select>
    </td>
  </tr>
</table>

I refactored your code a little:

  1. You can listen to any select change (not need to listen to each select separately).
  2. You can find the paragraph element using .closest.
  3. Trigger the change event to make the code to set the color when page load.

About sending it to the backend, well I don't know which server are you (want to) using but generally, you have 2 options: form submission or ajax (with jQuery).

Both of them send to the server a request with these params. After you get them in your server (maybe this question could help you with it), you can do with them anything you want, save them into DB or write them in file.

Let me know if something is missing.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • Can you have it populated with lots of text by default? Based on my question above, this is the most important part of the question. I think I need to upload a wireframe of what I'd really like to be able to do. – Eamonn Kenny Apr 19 '18 at 09:31
  • I've now added a diagram. Its probably a bit easier to follow now. – Eamonn Kenny Apr 19 '18 at 10:25
  • Regardless to your question, both of the editors can populate a lot of text. That's how CMS working these days.. About your question, the problems now are how to turn the highlight (and not the color) of the text AND how to send it to the backend, right? My question is where is the text come from? Are you store each of the paragraphs separate? Or you store all the text in 1 record and split it by breakline symbol? – Mosh Feu Apr 22 '18 at 08:04
  • the text was generated by a NLP application in python. The output results could be stored as CSV, JSON or any other appropriate format. The colour don't need to be actually included in the columns of the table, just a code that represents the colour. I just called them paragraphs so that its much easier to describe the problem to a non-expert. – Eamonn Kenny Apr 23 '18 at 08:39
  • Ok, so your questions are: A. How to make the highlight effect (e.g. text background)? B. How to send the color value (or code) to the server? – Mosh Feu Apr 23 '18 at 09:06
  • That probably sums it up. Thats what I tried to say in the title. – Eamonn Kenny Apr 23 '18 at 09:08
  • Ok, I just updated (re-writed it, to be more correctly) my answer. Please take a look.. – Mosh Feu Apr 23 '18 at 09:43
  • Thanks, so the start of what I was doing was okay, its just getting the framework now in place to generate the lines of HTML in a table in the correct format for user input. I think I'll get it working with the info you sent now. – Eamonn Kenny Apr 23 '18 at 10:43
  • My pleasure Good luck – Mosh Feu Apr 23 '18 at 11:26
  • Interestingly this works in jsfiddle but can't get it to work in a browser. Did you try it in a browser? The table appears but the colours never change. – Eamonn Kenny Apr 23 '18 at 12:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169590/discussion-between-mosh-feu-and-eamonn-kenny). – Mosh Feu Apr 23 '18 at 12:30