Here is code that I have copied from w3schools, I have different code, but the problem that I am having still happens in this simplified coding.
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to create an array, then display it's length</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var foods = ["steak","pizza","bread","fruits"];
var x=document.getElementById("demo");
var y=foods[3];
x.innerHTML= y.length;
}
</script>
</body>
</html>
When I do x.innerHTML= fruits.length;
, I get 4
back which is what I want to get.
But when I call
var y=foods[3];
x.innerHTML= y.length;
I get 6
which is the length of the word "fruits"
but I want the length of the array fruits
.
How do I do this?
I'm using jQuery, don't know if that affects anything. Do I have to add parenthesis or brackets somewhere?