Sunday 17 September 2017

How to use local variable (json data) in javascript file?

Can someone please help me find out what I am doing wrong, I have been stuck on this for waaaay to long now!I am trying to get data from a json file and use it in my javascript file. I am using a local variable by declaring a variable inn app.js. It works fine if I write: "<%= appledata.appels.apple2 %>" in my html code. But in javascript It does not..This is my code:function getPrice() { var applePrice; applePrice = appledata.appels.apple2; return applePrice;} function calculateTotPrice() { var TotPrice = getPrice(); var divobj = document.getElementById('totalPrice'); divobj.style.display='block'; divobj.innerHTML = fastPrisGardermoen; } appels.json:{ "appels": { "apple1": 10, "apple2": 15 } } app.js:app.locals.appledata = require("./appels.json"); html site:
all help is much appreciated!

Submitted September 17, 2017 at 02:41PM by stiros

No comments:

Post a Comment