Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

JavaScriptJQuery
Home›JavaScript›Check whether an array contains a particular element

Check whether an array contains a particular element

By SibeeshVenu
June 30, 2015
1184
0
Share:

Introduction

Hi All, How are you today? In this article we will see how we can check whether an array element is present in an array. We will be using JQuery to do this requirement. I hope you will like it.

Background

I know we all are working in the client side technologies, especially in JQuery. Sometimes we may need to write more client side codes rather than server side codes. In that case, you will be using client side arrays too. So if you use client side arrays,sometimes you may need to check the array contains a particular element or not.Then only you can do some codes according to that. Here I am going to give you a demo of how we can do this requirement.

Using the code

To start with, as you all know we need to load the JQuery reference.

[js]
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
[/js]

Once you load the reference you are ready to go.

Since this is a demo, we will explain with some steps. Sounds good?. So we will do the following tasks.

  • Add the elements to the array
  • Check the elements are added or not
  • Search an element
  • Shall we start then?

    Add the elements to the array

    We need to set our UI first right?

    [html]
    <body>
    Check whether an array contains a particular element – Sibeesh Passion
    <br/>
    <br/>
    <table>
    <tr>
    <td>
    <input type="text" id="myText" />
    </td>
    <td>
    <p id="addMe">Add Me</p>
    </td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td>
    <p id="showMe">Show Array Length</p>
    </td>
    <td id="showContent">Array length is
    </td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td>
    <input type="text" id="searchText" />
    </td>
    <td>
    <p id="searchMe">Search Me</p>
    </td>
    <td id="searchOutput">The given text </td>
    </tr>
    </table>
    </body>
    [/html]

    So we have set our UI, and now if you run your page you can see output as follows.

    www.sibeeshpassion.com

    Now we will add the needful scripts.

    [js]
    <script>
    $(document).ready(function() {
    var myArray = [];
    var i = 0;
    $("#addMe").click(function() {
    myArray[i] = $("#myText").val();
    $("#myText").val(”);
    i++;
    });
    $("#showMe").click(function() {
    $("#showContent").text("Array length is " + myArray.length);
    });
    $("#searchMe").click(function() {
    if (jQuery.inArray($("#searchText").val(), myArray) > -1)
    $("#searchOutput").text("The given text " + $("#searchText").val() + " is available in the array");
    else
    $("#searchOutput").text("The given text " + $("#searchText").val() + " is not available in the array");

    });
    });
    </script>
    [/js]

    As you can see we are adding elements to the array, checking the array element in the first two click functions. But what about the third click function. Bingo! there we are using jQuery.inArray function to check our element is present in the array or not.

    No we will learn little about the jQuery.inArray function.

    jQuery.inArray

  • It is used for searching a specified value within an array
  • It returns -1 if it does not contain the searched value
  • It returns index of the searched value if it contains the value
  • The following code block describes how we can use jQuery.inArray

    [js]
    if (jQuery.inArray($("#searchText").val(), myArray) > -1)
    $("#searchOutput").text("The given text " + $("#searchText").val() + " is available in the array");
    else
    $("#searchOutput").text("The given text " + $("#searchText").val() + " is not available in the array");

    [/js]

    Complete Code

    [html]
    <html>

    <head>
    <title>Check whether an array contains a particular element – Sibeesh Passion</title>
    <style>
    p {
    color: red;
    width: 170px;
    cursor: pointer;
    border: 1px solid #ccc;
    text-align: center;
    }
    </style>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    </head>

    <body>
    Check whether an array contains a particular element – Sibeesh Passion
    <br/>
    <br/>
    <table>
    <tr>
    <td>
    <input type="text" id="myText" />
    </td>
    <td>
    <p id="addMe">Add Me</p>
    </td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td>
    <p id="showMe">Show Array Length</p>
    </td>
    <td id="showContent">Array length is
    </td>
    </tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr>
    <td>
    <input type="text" id="searchText" />
    </td>
    <td>
    <p id="searchMe">Search Me</p>
    </td>
    <td id="searchOutput">The given text </td>
    </tr>
    </table>
    <script>
    $(document).ready(function() {
    var myArray = [];
    var i = 0;
    $("#addMe").click(function() {
    myArray[i] = $("#myText").val();
    $("#myText").val(”);
    i++;
    });
    $("#showMe").click(function() {
    $("#showContent").text("Array length is " + myArray.length);
    });
    $("#searchMe").click(function() {
    if (jQuery.inArray($("#searchText").val(), myArray) > -1)
    $("#searchOutput").text("The given text " + $("#searchText").val() + " is available in the array");
    else
    $("#searchOutput").text("The given text " + $("#searchText").val() + " is not available in the array");

    });
    });
    </script>
    </body>

    </html>
    [/html]

    Now we will run our page and see the output.

    Output

    www.sibeeshpassion.com

    www.sibeeshpassion.com

    www.sibeeshpassion.com

    www.sibeeshpassion.com

    That is all.

    Conclusion

    I hope you will like this article. Please share me your valuable thoughts and comments. Your feedback is always welcomed.

    Thanks in advance. Happy coding!

    Kindest Regards
    Sibeesh Venu

    Tagscheck arraycheck element in arrayJavascriptJQueryjquery functionsjquery inarray
    Previous Article

    Load Data on Scroll Using jQuery

    Next Article

    My Drawings

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • JQuery

      Copy contents to clipboard

      July 9, 2015
      By SibeeshVenu
    • jQuery Datatable With Server Side Data
      .NETAngularWeb API

      jQuery Datatable With Server Side Data

      February 25, 2016
      By SibeeshVenu
    • Code SnippetsJQuery

      Find An Inner Div Which Has A Particular Attribute And Remove

      August 14, 2015
      By SibeeshVenu
    • JQuery

      How to disable right click in a page using JQuery

      July 16, 2015
      By SibeeshVenu
    • Check for any unsaved changes in page
      CodeProjectHTMLHTML5JQuery

      Check for any unsaved changes in page

      September 29, 2015
      By SibeeshVenu
    • JQuery

      Load Contents From Text File using jQuery

      March 29, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version