I am trying to limit keyword input.
Whenever a user enter a keyword, it will append 'span' to that keyword, e.g.
like the html code below.
I can limit words, but the count is not accurate but when it reach 5, it stop entering.
If I refresh page, I can enter one more again, then stop me again too.
The weird things, it doesn't work if I use id for var count=$('#result_tag').text(). I have to use class.
I had tried to change /\S+/g to /\s+/g, not working, I have to keep S capital.
JQuery:
$( "#tag" ).on( "keydown", function( event ) {
var count=$('.result_tag').text();
var words=count.match(/\S+/g).length;
if(words >= 8 ){
alert('No more than eight words');
}
//save tag after press enter key
else if ( event.which==188 || event.which == 13 ) {
event.preventDefault();
//codes will do the action
HTML:
<span id="result_tag" class="result_tag">keyword that user enter</span>
<span id="result_tag" class="result_tag">keyword that user enter</span>
<span id="result_tag" class="result_tag">keyword that user enter</span>