Javascript string search string in array not working here is my code

Mycode

var array = ["foo","fool","cool","god",'searchstring'];
 var src_keyword="";
 $("#search_str").click(function(){
   src_keyword=$("#str_search").val();
  find(array,src_keyword)
 });

 function find(arr,src_keyword1) {
 var result = [];
 alert(src_keyword1);
//src_keyword1="oo";
for (var i in arr) {
    var search = new RegExp(src_keyword1, "gi");
    if (arr[i].match(search)) {
        alert(arr[i])
        result.push(arr[i]);

    }
 }

return result;
}

Html

 <input type="text" id="str_search" value=" "/>
 <input type="button" id="search_str" value="search" />

thanks in advance

I think you left out the display result bit....

$(document).ready(function(){

    var array = ["foo","fool","cool","god",'searchstring'];
    var src_keyword="";
    $("#search_str").click(function(){
        src_keyword=$("#str_search").val();
       result =  find(array,src_keyword);
       console.log(result);
    });
});

function find(arr,src_keyword1) {
    var result = [];
//    alert(src_keyword1);
    //src_keyword1="oo";
    for (var i in arr) {
        var search = new RegExp(src_keyword1, "gi");
        if (arr[i].match(search)) {
            result.push(arr[i]);
        }
    }
    return result;
}

see my post

 if (arr[i].match(search)) {
 alert(arr[i]);
 result.push(arr[i]);
 }
if (arr[i] == search) {
 alert(arr[i]);
 result.push(arr[i]);
 }

try it like this

sri,

As far as I could tell your original code worked, you just didnt output the result. I outputted the result to console on my line 8.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.