I am really really newbie in javascript and jquery. I am now making a calculator.
This is my code.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<meta charset="UTF-8">
<script type="text/javascript">
var buttonname = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "+", "-", "*", "/", "C", "Enter", "="];
$(document).ready(function() {
$("button").click(function() {
// for(var i=0;i<buttonname.length;i++){
var arrayvalue = buttonname[ ? ? ? ? ? ];
var calculate = $("<p>" + arrayvalue + "</p>");
$("#result").append(calculate);
//}
});
});
</script>
</head>
<body>
<!--<textarea id="numberarea"></textarea><br>-->
<script type="text/javascript">
for (var i = 0; i < buttonname.length; i++) {
document.write("<button>" + buttonname[i] + "</button>");
if (i != 0 && (i % 4) == 0)
document.write("<br>");
}
</script>
<div id="result"></div>
</body>
</html>
How should I do with line var arrayvalue = buttonname[] or with click function to get a specific value from the array in my case?
var arrayvalue = buttonname[x]wherexis the specific value you wish to retrieve, starting from 0.this.innerTextinstead ofbuttonname[?????]? Otherwise, store it as adata-attribute. If you're determined to reference it by index (by using thebuttonname[?]syntax), you'd have to somehow figure out the index of the button you pressed, but because it's nestled in with break tags, it'll be way more work than it's worth.