Jquery Inarray Example - Jquery

How to find the elements in array using jquery?

Snippet Code


  
Rate this page :
  [ 0 votes]

Inarray example code allows you to check whether the value exists in an array. If the value exists the code returns the array position, else it returns as -1.Simple jquery inarray code is given below.

<style> div {color: red;} span {color: green;} </style> <div>"car" is in the array <span></span></div> <div>4 is in the array <span></span></div> <div>"bus" is in the array <span></span></div> <div>"plain" is in the array <span></span></div> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script type='text/javascript'> var demoarr = ['car','bike','train','bus']; var ele = $("span"); ele.eq( 0 ).text( jQuery.inArray( "car", demoarr ) ); ele.eq( 1 ).text( jQuery.inArray( 4, demoarr ) ); ele.eq( 2 ).text( jQuery.inArray( "bus", demoarr ) ); ele.eq( 3 ).text( jQuery.inArray( "plain", demoarr, 2 ) ); </script>

Tags


Ask Questions

Ask Question