1

How do I select a word using Jquery and save that into DB. ie, Say I have the following in my page,

"<p>This is a beautiful sentence </p>"

If I click on 'beautiful', I want to show a pop up option "add". If I click on "add", the word "beautiful" will be saved in my existing database of words. If i click it again It wont be saved, the database contains unique words.

I am working in Django.

Edit: I tried the following:

<script>
$(document).ready(function(){
  $("p").click(function(){
    var value = $(this).text();
    var input = $('#my_input');
    input.val(value);
  });            
});    
</script>
Twisty
  • 30,304
  • 2
  • 26
  • 45
  • You'd want to wrap each part of the text that should be clickable with some type of element, like ``. This way you can attach a click callback to each wrapper. – Twisty Nov 15 '18 at 18:49
  • Can you explain a bit more? How do I add span ? Or any resource from where I can clearly learn this? –  Nov 15 '18 at 18:54
  • They are suggesting you change your markup to `

    This is a beautiful sentence

    `
    – Taplar Nov 15 '18 at 18:57
  • Exactly as was suggested by taplar. Your test code would call in all the text from `

    `, not just one word. There are difficult ways to identify a word by the location that was clicked, but I would advise finding a better way to tag the words.

    – Twisty Nov 15 '18 at 19:01

1 Answers1

1

Example:

$(function() {
  $("p.allow-click span").click(function() {
    var value = $(this).text();
    var input = $('#my-input');
    input.val(value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="allow-click"><span>This</span> is a <span>beautiful</span> <span>sentence</span>.</p>
<input id="my-input" type="text" />

As you can see, this allows each specific word to have a click event. You can use almost any tag, yet <span> is the least invasive in a sense. You could use <b>, <i>, or some other HTML Tag. These may change the type face or look of the text where <span> will not.

Update

If you do not want to manually edit the HTML, but do it all in jQuery, you could something like:

$(function() {
  function applyClick($t) {
    var inner = $t.text().trim();
    var trail = false;
    if (inner.slice(-1) == ".") {
      trail = true;
      inner = inner.slice(0, -1);
    }
    var parts = inner.split(" ");
    $.each(parts, function(k, v) {
      parts[k] = "<span>" + v + "</span>";
    });
    var myInner = parts.join(" ");
    if (trail) {
      myInner = myInner + ".";
    }
    $t.html(myInner);
    $t.find("span").click(function() {
      var value = $(this).text();
      var input = $('#my-input');
      input.val(value);
    });
  }
  applyClick($(".allow-click"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="allow-click">This is a beautiful sentence.</p>
<input id="my-input" type="text" />

Hope that helps.

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • So, do I need to add span manually? Or is there any other functions to do that? –  Nov 15 '18 at 19:20
  • @SubrotoPinku You could add them manually or you can program it. – Twisty Nov 15 '18 at 19:24
  • I have tried this, https://stackoverflow.com/questions/8609170/how-to-wrap-each-word-of-an-element-in-a-span-tag It doesn't help! :( –  Nov 15 '18 at 19:40
  • Thank You so much! Life saver! Okay, now can you please tell me any good resource to learn about all these? –  Nov 15 '18 at 20:26
  • @SubrotoPinku I like CodeAcedemy.com, they have a lot of good course. They have a JavaScript course you might like: https://www.codecademy.com/catalog/language/javascript – Twisty Nov 15 '18 at 21:05
  • 1
    @SubrotoPinku if this has answered your question, I hope you will mark it as the answer as well. – Twisty Nov 15 '18 at 21:07
  • Another thing I am encountering is, when I'am clicking it is taking '.' or any other punctuation at the end of a word. How do I avoid that? –  Nov 16 '18 at 15:39
  • 1
    @SubrotoPinku this is also included in my update above. Examine the code. – Twisty Nov 16 '18 at 15:44
  • What if there is multiple '...'? –  Nov 16 '18 at 15:48
  • 1
    @SubrotoPinku if there are multiple period symbols or other symbols, then you may want to perform a more complex search using regular expressions to find them. Since they can appear in any part of the sentence, you may want to capture the index position of each symbol and what the symbol is and then remove it from the sentence. Or don't, you can leave it in as part of the word and then filter it out before it appears in the text field. Give it a try and if needed, create a new question with what you have tried. – Twisty Nov 16 '18 at 16:06
  • This answer helped me, but when looping the list passed from controller or views, need to set the jquery code into the loop as well, and at the same time use dynamic id for fetching the elements in the looping part. It is a bit complicated, but still a lot thanks. – JonathanLu Jul 18 '21 at 22:54