Aspdotnet-Suresh

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 Remove Specific Element from Array by Value or Index with Example

Mar 16, 2015
Introduction

Here I will explain how to use
jQuery to delete or remove specific item or element from array by value or index with example or jQuery remove / delete specific element or item from array list with example. By using splice or grep properties in jQuery we can delete or remove specific item / element from array list.

Description:
  
In previous articles I explained
5 + jQuery flip effect plugin examples, 8 + jQuery page flip book plugin examples, jQuery zoom image on mouse over using zoom plugin, jQuery split string into array by comma, 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 delete or remove specific items from an array with example in jQuery.

By using splice or grep properties we can delete specific items from an array in jQuery for that we need to write the code like as shown below

Using grep property we can delete items like as shown below


<script type="text/javascript">
$(function () {
var arr = ["India", "USA","Australia","UK"];
$('#btnDelete').click(function () {
var str = $('#txtCountry').val();
arr = $.grep(arr, function (value) {
return value != str;
});
for (var i = 0; i < arr.length; i++) {
alert(arr[i])
}
});
})
</script>

Using splice property we can delete items like as shown below


<script type="text/javascript">
$(function () {
var arr = ["India", "USA", "Australia", "UK"];
$('#btnDelete').click(function () {
var str = $('#txtCountry').val();
for (var i = 0; i < arr.length; i++) {
if (arr[i] == str) {
arr.splice(i, 1);
}
}
for (var i = 0; i < arr.length; i++) {
alert(arr[i])
}
});
})
</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 delete items from array</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
var arr = ["India", "USA","Australia","UK"];
$('#btnDelete').click(function () {
var str = $('#txtCountry').val();
arr = $.grep(arr, function (value) {
return value != str;
});
for (var i = 0; i < arr.length; i++) {
alert(arr[i])
}
});
})
</script>
</head>
<body>
<div>
Enter Country Name: <input type="text" id="txtCountry" value="India" />
<input type="button" id="btnDelete" value="Delete Item" />
</div>
</body>
</html>

Live Demo

For live demo try to click below button it will delete “India” from list and display remaining items from array


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 RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

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

© 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.