Question

i need some code corrected. So here is the JS:

var $ = function (id) {
    return document.getElementById(id);
}
var myTransaction = new Array[];

function processInfo() {
    var myItem = $('item').value;
    var myAmount = parseFloat($('amount').value);
    var myTotal = myItem + ":" + myAmount;

    var myParagraph = $('message');

    myParagraph.innerHTML = myTransaction;


    for (var theTotal in myTransaction) {
        myTransaction += addTogether[theTotal] + "<br>";
    }
}


window.onload = function () {
    $("addbutton").onclick = processInfo;
}

and HTML

<section>
    <p>Item:
        <input type="text" id="item" size="30">
        <p>Amount:
            <input type="text" id="amount" size="30">
            <p><span id="message">*</span>

                <p>
                    <input type="button" id="addbutton" value="Add Item" onClick="processInfo();">
</section>

What i need to do is get the values of the text boxes, and add them together into one variable then have it stored into the array. Then use a for-in loop to concatenate every element in the Array into a one String variable. However, must also stick a
tag at the end every value in the String, last thing is place this String in a paragraph at the end of the page.

FIDDLE

Was it helpful?

Solution

If I understand you then look at my edition to your code: If you not understand something with the code so ask me.

At first you forget to close with ; the function $. then you create array with bad syntax. third you call addTogether array that doesn't exist. this is fixed code that i belive working like you asked.

var $ = function(id) {
    return document.getElementById(id);
};

var myTransaction = [];

function processInfo ()
{    
     var myItem = $('item').value;
     var myAmount = parseFloat($('amount').value);
     var myTotal = myItem + ":" + myAmount;
     var myParagraph = $('message');    

     myParagraph.innerHTML = "";
     myTransaction.push(myTotal);
     for (var theTotal in myTransaction)
     {
          myParagraph.innerHTML += myTransaction[theTotal] + "<br>"; 
     }

};

(function () {
    $("addbutton").onclick = processInfo;

})();

Edit
Mohamed-Ted suggest: instead of:

for (var theTotal in myTransaction)
{
     myParagraph.innerHTML += myTransaction[theTotal] + "<br>"; 
}

you can do this:

myParagraph.innerHTML += myTransaction.join("<br>"); 

see example on:
jsFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top