0

I did some searching around and found some issues similar to mine, but I was hoping for a resolution that corrects my pre-existing code.

Here's the codepen if you want to see the whole thing: http://codepen.io/JTBennett/pen/ygyZwE

$('#ST_txt_write').keyup(function(){
var childText = $('#ST_cmp_body').text();
var count = (childText.match(/hi/g) || []).length;
$('#ST_KW').html(count);
});

The issue is that I can't get the matching function to work with a textarea so the user can type whatever they want in the box and that will be what the code seeks to match. I tried making a variable that returns the text content of the box (just like the childText var you see there) but that would only match once and be done instead of counting.

Any help in the right direction would be most appreciated. I'm sorry I'm not very good with jQuery, or anything for that matter.

mhodges
  • 10,938
  • 2
  • 28
  • 46
Joel
  • 458
  • 3
  • 14
  • 1
    `.text()` is for elements that have innerText (i.e. `` `
    `, etc.). `
    – mhodges Jan 06 '17 at 18:17
  • what are you trying to match? – Aminur Rashid Jan 06 '17 at 18:17
  • Hi - I just tried val again (had that in there before) and it still only matches once: `$('#ST_txt_write').keyup(function(){` `var childText = $('#ST_cmp_body').text();` `var KW = $('#ST_KW_txt').val();` `var count = (childText.match(KW) || []).length;` `$('#ST_KW').html(count);` `});` – Joel Jan 06 '17 at 18:27

1 Answers1

2

The example you gave in your question has important differences from your codepen. In your question, you have a regex literal that you are passing into .match(), and in your codepen, you are passing a string into .match(). What you need to do is create a new regular expression using new RegExp() and pass it the global flag.

The code would look as follows:

$('#ST_txt_write').keyup(function(){
  var childText = $('#ST_cmp_body').val();
  var KW = $('#ST_KW_txt').val();
  var regex = new RegExp(KW, 'g'); // <--- This creates a regex from the string in KW
  var count = (childText.match(regex) || []).length; // <-- Use regex here instead of string
  $('#ST_KW').html(count);
});

Here's a working example:

$(function() {
  $('#ST_txt_write').keyup(function() {
    var childText = $('#ST_cmp_body').val();
    var KW = $('#ST_KW_txt').val();
    var regex = new RegExp(KW, 'g');
    var count = (childText.match(regex) || []).length;
    $('#ST_KW').text(count);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ST_txt_write">
  <textarea id="ST_cmp_body" style="height: 200px; width: 300px;"></textarea>
</div>
<div>
  Keywords:
  <input type="text" id="ST_KW_txt" />
</div>
<div>
  # of Keywords Found: <span id="ST_KW"></span>
</div>
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • Very cool, thank you for interpreting my question correctly too - I must've worded it poorly. I had to change the childText variable to .text instead of .val since that was actually monitoring the preview div instead of the composing textarea, but it works perfectly. I can also build on this code to get a few other things done too, so thanks for that. – Joel Jan 06 '17 at 19:09
  • @Joel Ah, that makes sense about using `.text()` now since you are extracting it from the div, rather than the textarea. I will update my answer accordingly. Glad this solution worked for you! – mhodges Jan 06 '17 at 19:24