admin管理员组文章数量:1340367
I want to make a button that changes the value of an element in an array. I am trying to do it by the following code but the element does not change. As a self learning beginner, I am probably missing something very obvious and I would appreciate if someone can point that out to me.
Thank you for your answers!
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>
</head>
<body>
<textarea id="log2"></textarea>
<input type="button" onClick="uClicked();" value="Click!">
<script>
var fer=[];
for (i=0; i< 15; i++){
fer[i]=i+1;
}
function uClicked(fer){
fer[12] = 10;
return fer[12];
}
log2.value = "fer[12]= " + fer[12];
</script>
</body>
</html>
I want to make a button that changes the value of an element in an array. I am trying to do it by the following code but the element does not change. As a self learning beginner, I am probably missing something very obvious and I would appreciate if someone can point that out to me.
Thank you for your answers!
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>
</head>
<body>
<textarea id="log2"></textarea>
<input type="button" onClick="uClicked();" value="Click!">
<script>
var fer=[];
for (i=0; i< 15; i++){
fer[i]=i+1;
}
function uClicked(fer){
fer[12] = 10;
return fer[12];
}
log2.value = "fer[12]= " + fer[12];
</script>
</body>
</html>
Share
Improve this question
asked May 21, 2012 at 17:47
glin yonglin yon
991 gold badge2 silver badges8 bronze badges
1
- related: jqfundamentals./#example-2.46 – jbabey Commented May 21, 2012 at 17:58
2 Answers
Reset to default 6function uClicked(){ // remove the parameter.
The parameter isn't needed, and is hiding the real fer
variable.
Because fer
was declared in outer scope, uClicked
function can access it.
Fixed code:
var fer=[];
for (i=0; i< 15; i++){
fer[i]=i+1;
}
function uClicked(){
fer[12] = 10;
alert(fer[12]);
}
Your code with ments
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>
</head>
<body>
<textarea id="log2"></textarea>
<input type="button" onClick="uClicked();" value="Click!">
<script>
var fer=[];
for (i=0; i< 15; i++){ //use var i, otherwise you are putting i in the global scope
fer[i]=i+1;
}
function uClicked(fer){ // fer is undefined because you are not passing argument when you call the function
fer[12] = 10;
return fer[12];
}
log2.value = "fer[12]= " + fer[12]; //log2 is not defined.
</script>
</body>
</html>
The working code:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Change Array Value</title>
</head>
<body>
<textarea id="log2"></textarea>
<input type="button" onclick="uClicked();" value="Click!" />
<script type="text/javascript">
var fer = [];
for(var i; i < 15; i++){
fer[i]=i+1;
}
var log2 = document.getElementById("log2");
function uClicked(){
fer[12] = 10;
log2.value = "fer[12]= " + fer[12];
return fer[12];
}
</script>
</body>
</html>
本文标签: How to change value of an array element through javascriptStack Overflow
版权声明:本文标题:How to change value of an array element through javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743633912a2513575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论