<code>
<!DOCTYPE html>
<html>
<head>
<title>Java Script Array methods</title>
<script type="text/javascript">
var ArrStr = ["Yallappa","Rajesh","Kiran"];
function LoadArr(){
document.getElementById("Arr").innerHTML = ArrStr;
}
function But1Click(){
ArrStr.push(document.getElementById("TxtBox").value);
document.getElementById("Arr").innerHTML = ArrStr;
}
function But2Click(){
var Indx = ArrStr.indexOf(document.getElementById("Arr").value);
alert(Indx);
if(indx > -1){
arrstr.splice(document.getelementbyid("arr").value,1);
}
document.getelementbyid("arr").innerhtml = arrstr;
}
</script>
</head>
<body onLoad="LoadArr()">
<h2 id="Arr"></h2><br>
<input type="text" id="TxtBox"/><br>
<input type="button" onclick="But1Click()" text="Push into Array" value="Push into Array"/><br>
<input type="button" onclick="But2Click()" text="Remove from Array" value="Remove From Array"/>
</body>
</html>
</code>
Asked
Active
Viewed 41 times
-2

Yallappa Bestha
- 1
- 2
-
1JS is case sensitive. In your But2click() function you have used "Indx" in assignment statement and "indx" in if () statement – jophab Sep 11 '16 at 14:49
-
You've got a lot of case errors – Andrew Li Sep 11 '16 at 14:50
1 Answers
0
In But2Click
method change arrstr
to ArrStr
and indx
to Indx
Your But2Click
is full of errors. It should be as given below:
function But2Click(){
var Indx = ArrStr.indexOf(document.getElementById("TxtBox").value); //id here should be of textbox
alert(Indx);
if(Indx > -1)//case of variable was incorrect
{
ArrStr.splice(Indx,1); //splice function needs index as first argument
}
document.getElementById("Arr").innerHTML = ArrStr;//id was incorrect and case in variable name was incorrect
}

Akshey Bhat
- 8,227
- 1
- 20
- 20
-
Thank you, I have corrected them, But still I am not able to remove string. Every time in the alert it is showing index as -1. – Yallappa Bestha Sep 11 '16 at 16:02
-
@YallappaBestha there were a lot of errors in your code which i had overlooked. Check my modified answer. – Akshey Bhat Sep 12 '16 at 04:16