I'm working on a calculator application. I created the following function to push the the current function to a div to display itself.
function addtoScreen(vTag){
var screen = document.getElementById("screen");
if(currentFunction.length == 1){
var newCalc = document.createElement("p");
newCalc.setAttribute("class", "calc");
var opInt = "<span>"+vTag+"</span>";
newCalc.innerHTML = opInt;
screen.innerHTML = newCalc;
}else{
var opInt = "<span>"+vTag+"</span>";
newCalc = document.getElementById("screen").lastElementChild;
newCalc.innerHTML = opInt;
}
}
The if portion keeps returning [object HTMLParagraphElement] what am I doing wrong? I have tried to use .value but that just returns a null because the object is a entire element that does not have a value attribute.
Edit: I didn't mention that vTag is just a string being passed through from a function that pushs to a array.
I'm working on a calculator application. I created the following function to push the the current function to a div to display itself.
function addtoScreen(vTag){
var screen = document.getElementById("screen");
if(currentFunction.length == 1){
var newCalc = document.createElement("p");
newCalc.setAttribute("class", "calc");
var opInt = "<span>"+vTag+"</span>";
newCalc.innerHTML = opInt;
screen.innerHTML = newCalc;
}else{
var opInt = "<span>"+vTag+"</span>";
newCalc = document.getElementById("screen").lastElementChild;
newCalc.innerHTML = opInt;
}
}
The if portion keeps returning [object HTMLParagraphElement] what am I doing wrong? I have tried to use .value but that just returns a null because the object is a entire element that does not have a value attribute.
Edit: I didn't mention that vTag is just a string being passed through from a function that pushs to a array.
Share Improve this question edited Nov 11, 2014 at 16:50 Mike F asked Nov 11, 2014 at 16:41 Mike FMike F 3091 gold badge8 silver badges19 bronze badges 4- 1 please add your html, optionally as snippet. – ps2goat Commented Nov 11, 2014 at 16:43
-
1
vTag
is a DOM element? You should be able to usevTag.outerHTML
to get its content – James Thorpe Commented Nov 11, 2014 at 16:43 -
1
It looks like you are setting your
currentFunction.length = 1
rather than paring the length to 1currentFunction.length == 1
. – zack.lore Commented Nov 11, 2014 at 16:45 - I've made the adjustment – Mike F Commented Nov 11, 2014 at 16:51
1 Answer
Reset to default 3How about something like this (I mented out your old code):
function addtoScreen(vTag){
var screen = document.getElementById("screen");
if(currentFunction.length == 1){
var newCalc = document.createElement("p");
//newCalc.setAttribute("class", "calc");
newCalc.className = "calc";
//var opInt = "<span>"+vTag+"</span>";
var opInt = document.createElement("span");
opInt.innerHTML = vTag;
//newCalc.innerHTML = opInt;
newCalc.appendChild(opInt);
//screen.innerHTML = newCalc;
screen.appendChild(newCalc);
}else{
//var opInt = "<span>"+vTag+"</span>";
var opInt = document.createElement("span");
opInt.innerHTML = vTag;
newCalc = screen.lastChild;
//newCalc.innerHTML = opInt;
if(newCalc)
{
newCalc.appendChild(opInt);
}
else
{
screen.appendChild(opInt);
}
}
}