I am trying to get the data and display from nested array through javascript
i have created an array like this
var products = {Elecrtonics:["Mobile","Tablet","Laptop"],
fasion:["celio","UCB","PE"],sports:["Puma","Rebock","Nike"]
};
i have three buttons named Electronics, Fasion, Sports
when i click on Electronics button it should fetch and display electronics array ["Mobile","Tablet","Laptop"]
same for Fasion and Sports
What should my approach for this? Or am i going wrong some where?
My Entire of HTML and JS looks like this
<div class = "container" id="formcontainer">
<form>
<div class="form-group col-lg-4 col-md-offset-3">
<h1>Welcome</h1>
<input type="text" class="form-control search" id="search" placeholder="What is in your mind?">
</div>
<div class="form-group col-lg-4 col-md-offset-3">
<button class="btn btn-primary" onClick="getData(Electronics)">Elecrtonics</button>
<button class="btn btn-primary" onClick="getData(Fasion)">Fasion</button>
<button class="btn btn-primary" onClick="getData(Sports)">Sports</button>
</div>
<p id = "myProducts"></p>
</form>
</div>
<script>
var products = {Elecrtonics:["Mobile","Tablet","Laptop"],fasion:["celio","UCB","PE"],sports:["Puma","Rebock","Nike"]};
function getData (el){
document.getElementById("myProducts").innerHTML = products.el;
event.preventDefault()
}
</script>