简体   繁体   中英

Using Variable Within JavaScript Object Literal

Basically, I'm having trouble getting information from an input box and using that variable within an object literal.

Here is my code:

$("document").ready(function() {

    function updateEvent(){
        render();
    }

    function render() {
                     //get values from input
            var gallons = $("#gallons").val();
            var electricity = $("#electricity").val();
            var energy = $("#energy").val();

        var figure = new Highcharts.Chart({

            series: [{
                    name: 'Throttle',
                    data: [0, 0, 30 , 0, energy, 30, 0, 0] //Variables will not work in here, no specific error but data doesn't show
                }, {
                    name: 'Power',
                    data: [30, 30 , 30, 30, 30, 30, 30, 30]
                }]

        });

I've tried embedding a function and doing other tricks but nothing has worked, I could be doing it wrong, I'm not sure. Is there any way to use a variable within an object literal without completely re-coding the structure.

Any help would be greatly appreciated.

How to get a value from an input box and use that value within an object literal?

function doStuff(){
      var realIncome = document.getElementById("income").value;
      var objLit= { realIncome: realIncome,
                 desiredIncome: realIncome * 4 };
      console.log(objLit);
      document.getElementById("out").innerText = 
            objLit.realIncome + " " + objLit.desiredIncome;
 }

And the HTML

Big Number 
<input id="income" name="income" type="number" 
    placeholder="please enter your desired income">
<button name="calculate" onclick="doStuff()">click me </button>​

Your income <span id="out"></span><br />

Please take a look at my demo code: http://jsfiddle.net/mppAM/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM