I am using this code to removes array item. I want to remove array items from arraylist on each click. When i clicked on Get Value, it gives 3 values, once got the items, need to remove these items on each click. I am new to jQuery , can anybody please tell me how do i remove items on each click.
This is jquery n javascript code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
var i =0; var ListOfArray = [];
$("#get_value").click(function ()
{ //causing error here removed the array in name value
var option = $('input:text[name=name1]').map(function() {
return $(this).val();
}).get().join();
$("input:text[name=name1]").val("");
ListOfArray.push(option);
for (var i= 0; i < ListOfArray.length; i++){
var newList = "<a href='#' onClick='removeArray(" + i + ");return true;'> DELETE </a> " + option + " <br>";alert(newList);
};
document.getElementById('myDiv').innerHTML += newList;
return false;
});
function removeArray(i)
{
alert('after removed array.'+i);
ListOfArray.splice(i,1);
var newList = "";
console.log(ListOfArray);
for (i = 0; i < ListOfArray.length; i++){
//You refer to option here for element, which should be replaced by proper index of array
newList += "<a href='javascript:void(0)' onClick='removeArray(" + i + "); return false;'> DELETE </a> " + ListOfArray[i] + " <br>";
};
document.getElementById('myDiv').innerHTML = newList;
return true;
}
</script>
HTML code
<div id="array_container">
<label>Name</label>
<input type="text" name="name1" value="" />
<label>State</label>
<input type="text" name="name1" value="" />
<label>Color</label>
<input type="text" name="name1" value="" />
<input type="button" name="get_value" id="get_value" value="Get Value"/>
</div>
<div id="myDiv"></div>