Mike F
Mike F

Reputation: 309

javascript returning object htmlelement

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.

Upvotes: 0

Views: 12520

Answers (1)

zack.lore
zack.lore

Reputation: 527

How about something like this (I commented 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);   
      }

  }

}

Upvotes: 3

Related Questions