1

I want to check if the value of my textarea is almost the same as requires , for example :

I have a HTML code :

<textarea class="ab" placeholder="Type here"></textarea>
<div class="result">
</div>

and a Jquery code :

$(document).ready(function(){

  $(".btn").click(function(){
  var a = $(".ab").val();
  var b = $(".result").html();
    /* */



if(a.indexOf('Which of the following is generally true about Open Source software?') >= 0){$('.result').html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');}    /* */
    else{
      $(".result").html("error");

    }
  });

}); 

this code doesn't work as what i want actually , this is just what i tried to make . But the thing i want is for example when the value of the $('.ab') is almost the same as the text Which of the following is generally true about Open Source software? like the following is generally true or true about the Open Source , the $(".result") still have the html as Its usually developed on a volunteer basis and anyone is free to change it (correct).

So how do i do that , thanks for your help

AustinP
  • 95
  • 9

2 Answers2

1

Try splitting input text into array , using $.each() to iterate input words , if input words match at least five words in selected phrase , return true , else return false at if ; e.g.; try typing or pasting at textarea

the following is generally true or true about the Open Source

$(document).ready(function() {

  $(".btn").click(function() {
    var a = $(".ab");
    var b = $(".result");
    var arr = a.val().split(/\s+/);
    var n = 0;
    var phrase = "Which of the following is generally true about Open Source software?";
    $.each(arr, function(key, val) {
      if(phrase.indexOf(val) >= 0) ++n;
    })
  
    if (n >= 5) {
     b.html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');
    }
    else {
      b.html("error");
    };
    a.val(""); n = 0;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<textarea class="ab" placeholder="Type here"></textarea>
<div class="result"></div>
<button class="btn">click</button>
guest271314
  • 1
  • 15
  • 104
  • 177
  • well ,thanks but it actually doesn't work with the string " following is generally true " . – AustinP Oct 18 '15 at 21:19
  • @user3722325 " following is generally true " is four words , tried with "the following is generally true" ? Or, adjust `if` condition to `n >= 4` . Note , does not check if previous words is current word when incrementing match count ; i.e., "true true true true true" might return `true` ; though that portion could be adjusted to test for matches of different words – guest271314 Oct 18 '15 at 21:24
  • @user3722325 See also http://stackoverflow.com/questions/23305000/javascript-fuzzy-search-that-makes-sense/ – guest271314 Oct 18 '15 at 21:29
0

Actually it should be:

    $(document).ready(function(){
        $(".btn").click(function(){
            var a = $(".ab").val();
            var b = $(".result").html();
            var c = 'Which of the following is generally true about Open Source software?';
            console.log(c.indexOf(a));
            if(c.indexOf(a) >= 0){
                $('.result').html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');
            } else {
                $(".result").html("error");
            }
        });

    }); 

<textarea class="ab" placeholder="Type here">following is generally true about Open Source</textarea>
<div class="result"></div>
<button class="btn">test</button>
Jaya Vishwakarma
  • 1,332
  • 1
  • 18
  • 36
  • it doesn't work for me when i test with the string : "Which of the following is generally" , it doesn't return to the result " Its usually developed on a volunteer basis and anyone is free to change it (correct). " [jsbin](http://jsbin.com/kayibe/edit?html,css,js,output) – AustinP Oct 18 '15 at 21:18
  • It returned "Its usually developed on a volunteer basis and anyone is free to change it (correct)." for me. – Jaya Vishwakarma Oct 18 '15 at 21:31