JQuery Add Items (Element) To Array If Not Exists In List With Example

Aspdotnet-Suresh
ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Add Items (Element) to Array If Not Exists in List with Example

Mar 15, 2015 Introduction: Here I will explain how to use jQuery to add items or elements to an array if not exists in list with example. By using inArray property we can check whether elements exists or not in array using jQuery. Description: In previous articles I explained jQuery currency exchange rate example with google api, jQuery referesh div without reloading page, jQuery delete row from gridview and database in asp.net, jQuery check if string contains specific text or not and many articles relating to AngularJS, jQuery, JavaScript and asp.net. Now I will explain how to add items to an array if not exists in array with example in jQuery. By using inArray property we can add items to an array if not exists in jQuery for that we need to write the code like as shown below
<script type="text/javascript"> $(function () { var arr = ["test","test1"]; $('#btnInsert').click(function () { var str = $('#txtCountry').val(); var stats = $.inArray(str, arr); if (stats == -1) { arr.push(str); } else { alert('Item Already Exists') } }); }) </script>
If you want to check it in complete example you need to write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jquery Add Items or elements to Array if not exists</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> <script type="text/javascript"> $(function () { var arr = ["test","test1"]; $('#btnInsert').click(function () { var str = $('#txtCountry').val(); var stats = $.inArray(str, arr); if (stats == -1) { arr.push(str); for (var i = 0; i < arr.length; i++) { alert(arr[i]) } } else { alert('Item Already Exists') } }); }) </script> </head> <body> <div> Enter Country Name: <input type="text" id="txtCountry" value="India" /> <input type="button" id="btnInsert" value="Insert Item" /> </div> </body> </html>
Live Demo For live demo try to enter same country names in textbox and click button to check whether item exist in array list or not to insert into array list
Enter Country Name:
Protected by Copyscape Unique Content Checker

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

Newer Post Older Post Home Subscribe to: Post Comments ( Atom )
  • Interview Questions in ASP.NET, C#, VB.NET, SQL, .NET Framework
  • Simple login form example in asp.net Check Username and Password availability in database
  • Asp.net insert, Edit, update, delete data in gridview
  • Introduction to Object Oriented Programming Concepts (OOPS) in C#.net
  • 3 tier architecture example in asp.net with C#
  • OOPS (Object Oriented Programming) Concepts in C#.NET with Examples
  • how to insert images into database and how to retrieve and bind images to gridview using asp.net (or) save and retrieve images from database using asp.net
  • Best Login Page Design in HTML, CSS with Source Code
  • C# - Constructors in C# with Example, Types of Constructor in C# with Example
  • Introduction to WCF - WCF tutorial | WCF Tutorial - Windows Communication Foundation | WCF Example | WCF Sample code in asp.net 3.5 | Basic WCF Tutorial for Beginners
Search This Site
Follow US
My Photo Suresh Dasari View my complete profile
Protected by Copyscape Duplicate Content Finder
Get Latest articles in your inbox for free.
Enter your email address:

Tags

Asp.net JQuery C#.Net General VB.NET Code Snippets Javascript SQL Server Gridview asp.net mvc c# JQuery Plugins Errors Interview Questions Fileupload Ajax mvc DropdownList AngularJS JSON validations Google API AutoComplete Google MAPS CSS DatePicker Windows Application IISServer Modalpopup Membership Authentication CheckBox Crystal Reports HTML ExcelSheet OOPS Concepts SharePoint jQuery UI ExportGridviewData XML SendMail WebService Entity Framework UpdatePanel AjaxModalPopupExtender Bootstrap Google Maps API InternetTips SlideShow ToolTip Visual Studio WCF Menu Progressbar Razor View SQL Joins web.config ADO.NET DataGridview Repeater Web API YouTube AjaxAsyncFileUpload Cookie DataList EncryptionandDecryption LightBoxEffect RDLC Report RadioButtonList Windows Service loading Image Accordion Menu Charts CheckBoxList Dynamic Controls Facebook Fixed Header on Scroll Generic List Global.asax IEnumerable SSRS Twitter jQuery Menu Access Database Blog Statistics KeyBoard Key Codes ListBox MultilineTextbox Polymorphism UI Grid ZIP UNZIP Files jQuery Cookie setInterval setTimeOut 360 Degree View Plugins ASP AjaxAutoCompleteExtender AjaxTabContainer Average rating Captcha Flip Effect HTML5 Installer LINQ Linkedin MySQL News Ticker in jQuery PDF Viewers Product Reviews QR Code QueryString Reflection Resize Image Reviews SQL Constraints SQL Server 2008 R2 Session Timeout SiteMap Social Media Bookmark Plugins ThumbnailsGeneration UserName Check Visitors Count Windows 8 app.config asp.net core jQuery Media Plugins sorting windows 10 3-TierArchitecture AbstractVsInterface ActiveDirectory Advertise Ajax Calendarextender Ajax ConfirmbuttonExtender AjaxAccordionControl AjaxCalendarExtender AjaxCollapsiblePanelControl AjaxDragPanelExtender AjaxPasswordStrength AjaxRatingControl AjaxSlideshowExtender Arraylist Assembly Authorization Caching Chatting Plugins CodingStandards Containers Custom Right Click Menu Dapper DataGrid Docker Donate Error Log Forums Google Charts Gzip Compression Hyperlinks IP Address ImportContacts Lync MCC Award MVP Award Northwind Database Notification Bar Panorama Image Viewer Plugins Print DIV Projects Query Strings RSSFeeds Read/Write text file ReadOnlyValues RichTextBox Scrollbar Session Setup File Spell Checker TFS Testimonial Example Testing TextArea Trace Mobile Number Try Catch VBScript Virtual Keyboard WPF bulk copy code contactus delegates dynamically page create generate script jQuery Audio Plugins jQuery Mobile jQuery Video Plugins jqGridview scroll top/bottom of div slider from folder tree view © 2015 Aspdotnet-Suresh.com. All Rights Reserved. The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.

Tag » Add Element Jquery Array