Andrey
Andrey

Reputation: 859

.innerHTML function Javascript

This is my code:

function text(var text)
{
  var Value = document.getElementById("display").innerHTML;
  var New = Value + text;
  document.getElementById("display").innerHTML=New;
}

What it's supposed to do is get a string from when it's called, and then add that string to the end of a div element with the ID "display". I called it like this: text("hello");

The HTML of the webpage is a blank div element, and it stays blank even after the code is run. It worked when I did document.getElementById("display").innerHTML="hello!";, but isn't now. Thanks!

Upvotes: 0

Views: 2535

Answers (3)

Nation
Nation

Reputation: 506

And you can do it like this:

function text(inputText)
{
  document.getElementById("display").innerHTML = document.getElementById("display").innerHTML +" "+ inputText;
}

:)

Upvotes: 0

Craig O
Craig O

Reputation: 852

Take the var out of the function parameter: function text(text)...

BTW: don't name your parameter the same thing as your function - it's confusing.

Upvotes: 3

Brilliand
Brilliand

Reputation: 13714

Don't use "var" for a function parameter - just listing it between the function parenthesis is enough. So change that function to:

function text(text)
{
  var Value = document.getElementById("display").innerHTML;
  var New = Value + text;
  document.getElementById("display").innerHTML=New;
}

and it should work.

Upvotes: 3

Related Questions