I want to remove array item on each click. Onclick of get_values button, displays array items in front of delete button/link. I need to remove those item on each click of delete button. It deletes whole item, but i want one by one. Plz help me. I am new to jquery.
<html>
<body>
<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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--<script src="../js/jquery.min.js"></script>-->
<script type="text/javascript">
$("#get_value").click(function ()
{ //causing error here removed the array in name value
var ListOfArray = [];
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++)
{
alert(i); //alert(ListOfArray.length);
var newList = "<a href='#' onClick='removeArray(" + i + ");'return false;> DELETE </a> " + option + " <br>";
alert(newList); //alert(i);
};
document.getElementById('myDiv').innerHTML += newList;
return true;
});
function removeArray(i)
{
var ListOfArray = [];
alert('after removed array.'+i);
ListOfArray.splice(i,1);
var newList = "";
//console.log(ListOfArray);
for (var i = 0; i < ListOfArray.length; i++)
{ //You refer to option here for element, which should be replaced by proper index of array
alert(ListOfArray.length); alert(i);
newList += "<a href='#' onClick='removeArray(" + i + ");'return false;> DELETE </a> " + ListOfArray[i] + " <br>";
alert(i);
};
document.getElementById('myDiv').innerHTML = newList;
return true;
}
</script>
</body>
</html>
var ListOfArray = [];to global scope. in simple language write the statement outside functions and only once.