use variable in pattern

259
November 21, 2016, at 9:27 PM

i am new to jquery I have a simple html textarea and I want to count specific word in text area and show that count and also highlight that 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, do 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, do 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, do 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, do 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>

READ ALSO
onchange in designed select doesn&#39;t work

onchange in designed select doesn't work

I have this little problem and I am asking if maybe can somebody help me. .

342
one dropdown close on other dropdown opening bootstrap

one dropdown close on other dropdown opening bootstrap

I have a navigation menu which structure goes as:.

303
Jquery unbind events

Jquery unbind events

I have two scripts.

401
How to individually increase or decrease a value using jquery

How to individually increase or decrease a value using jquery

I am looking to increase or decrease each products by clicking + or - button individually, but the issues is when I click any + button. .

351