0

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>
2
  • 1
    Move var ListOfArray = []; to global scope. in simple language write the statement outside functions and only once. Commented Sep 24, 2014 at 12:43
  • 2
    posted 2 days ago ? how-to-remove-array-items-from-array-list Commented Sep 24, 2014 at 12:51

1 Answer 1

1

Currently ListofArray is local variable to the onclick handler function and removeArray function cant access it from the outer scope. In addition you have declared a new variable inside removeArray . What you need to do is to keep the ListofArray variable as a single instance which is shared by both the functions.

Like

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++)
	{  
		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)  
{   
	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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.