javascript - use variable in pattern -
i new jquery have simple html textarea , want count specific word in text area , show count , highlight text too.
$(document).ready(function() { $(".text").on('input',function(){ var a= $(".text").val(); //var a="i m running"; /*if(a==="nokia"){ alert("nokia found"); } else{ alert("not found"); }*/ var pattern = /nokia/; var pattern1 = /samsung/; var pattern2 = /iphone/; var pattern3 = /qmobile/; //returns true or false... var exists = pattern.test(a); var exists1 = pattern1.test(a); var exists2 = pattern2.test(a); var exists3 = pattern3.test(a); if(exists){ //true statement, whatever //alert("nokia"); $(".nokia").css("background-color", "green"); } else{ //false statement..do whatever //alert("not nokia"); $(".nokia").css("background-color", "red"); } if(exists1){ //true statement, whatever //alert("samsung found"); $(".samsung").css("background-color", "green"); } else{ //false statement..do whatever // alert("not samsung found"); $(".samsung").css("background-color", "red"); } if(exists2){ //true statement, whatever //alert("samsung found"); $(".iphone").css("background-color", "green"); } else{ //false statement..do whatever //alert("not samsung found"); $(".iphone").css("background-color", "red"); } if(exists3){ //true statement, whatever // alert("samsung found"); $(".qmobile").css("background-color", "green"); } else{ //false statement..do whatever //alert("not samsung found"); $(".qmobile").css("background-color", "red"); } }); counter = function() { var value = $('#text').val(); if (value.length == 0) { $('#wordcount').html(0); $('#totalchars').html(0); $('#charcount').html(0); $('#charcountnospace').html(0); return; } var regex = /\s+/gi; var wordcount = value.trim().replace(regex, ' ').split(' ').length; $('#wordcount').html(wordcount); }; $(document).ready(function() { $('#text').keydown(counter); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <textarea id='text' style="width: 50%;height: 20%;" type="text" class="text"/></textarea> <button name="click" class="btn"/>click</button> <div id="result"> words: <span id="wordcount">0</span><br/> </div> <ul> <li class="nokia">nokia</li> <li class="samsung">samsung</li> <li class="iphone">iphone</li> <li class="qmobile">qmobile</li> </ul>
Comments
Post a Comment