enltr xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#"http://www.w3.org/1999/xhtml/vocab/////////////////Search for value in array with .inarray() | BitsyCodehttps://www.bitsycode.com/methods/dom/inarraySearch for value in array with .inarray() | BitsyCode Search for value in array with .inarray() | BitsyCode html not-front not-logged-in no-sidebars page-node page-node- page-node-20 node-type-page

Search for value in array with .inarray()

container

.inarray()


Description: Search within your arrays for specific values.


Example:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>inarray demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
        
      <table cellpadding="10" cellspacing="0" class="blocks">
          <tr>
              <td width="150" valign="top"><b>INARRAY:</b></td>
              <td><input type="text" name="value_inarray" class="value_inarray" value="" /></td>
          </tr>
          <tr>
              <td valign="top"><b>TEST ARRAY:</b></td>
              <td>['HTML5', 'CSS3', 'jQuery', 'HTML', 'CSS', 'HTML5', 'HTML5CSSJQUERY']</td>
          </tr>
          <tr>
              <td valign="top"><b>Options:</b></td>
              <td>
                  <div class="form-group" style="margin-bottom:0px">
                      <input type="checkbox" name="any" id="any" value="true" />
                      <label for="any">
                        <span>Any<br />
                        <small>Checks for word anywhere in string <b>(overrides "Exact match")</b></small></span>
                      </label>
                  </div>
                  <div class="form-group">
                      <input type="checkbox" name="exact" id="exact" value="true" /> 
                      <label for="exact">
                        <span>Exact match<br />
                        <small>Check for exact match, using case sensitivity <b>(case sensitive)</b></small></span>
                      </label>
                  </div>
              </td>
          </tr>
          <tr>
              <td></td>
              <td><input type="button" name="submit" onclick="inarray();" value="Validate" /></td>
          </tr>        
          <tr>
              <td valign="top"><b>Results:</b></td>
              <td><span class="results"></span></td>
          </tr>           
      </table>         
      
      <script>
         
      function inarray() {
         
         var exact_checked = jQuery("input[name=exact]").is(":checked");
         var any_checked = jQuery("input[name=any]").is(":checked");
           
         if (bitsyCode(".value_inarray").val().length == 0) {
             
             alert("Please enter a keyword.");
             bitsyCode(".value_inarray").focus();
             
         } else {
             
             var testArray = ['HTML5', 'CSS3', 'jQuery', 'HTML', 'CSS', 'HTML5', 'HTML5CSSJQUERY'];
             var results = bitsyCode.inarray(bitsyCode(".value_inarray").val(), { array:testArray, any:any_checked, exact:exact_checked });                
             
             var matches = "";
             for(var i=0; i < results.matches.length; i++){
                 matches += "<p><b>String: </b>" + results.matches[i].string + "<br />";
                 matches += "<b>Position: </b>" + results.matches[i].position + "</p>";
             }
             
             var response = "<b>Matches found:</b> " + results.found + "<br />";
                 response += "<b>Number of matches found:</b> " + results.count + "<br /><br />";
                 response += "<b>Matches:</b> " + matches;
                 
             bitsyCode(".results").html(response);
             
         }
      }
         
      </script>
     
    </body>
    </html>

Preview:

INARRAY:
TEST ARRAY: ['HTML5', 'CSS3', 'jQuery', 'HTML', 'CSS', 'HTML5', 'HTML5CSSJQUERY']
Options:
Results:
Share

Developed and built with passion and commitment.
Maintained by the bitsycode team.