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
Home
Asp.Net
Ajax
Gridview
JavaScript
SQL
JQuery
OOPS Concepts
MVC
Interview Questions
Tutorials
Trace Mobile
jQuery Add Items (Element) to Array If Not Exists in List with Example
By: Suresh Dasari Mar 15, 2015 Categories: Code Snippets , Javascript , JQuery 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
If you want to check it in complete example you need to write the code like as shown below
<htmlxmlns="http://www.w3.org/1999/xhtml"><head><title>jquery Add Items or elements to Array if not exists</title><scripttype="text/javascript"src="http://code.jquery.com/jquery-1.8.2.js"></script><scripttype="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: <inputtype="text"id="txtCountry"value="India"/><inputtype="button"id="btnInsert"value="Insert Item"/></div></body></html>
Live DemoFor 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:
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 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