0

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!

3
  • 2
    ...so what's your question? I can't see what you need help with... Commented May 1, 2012 at 2:30
  • 1
    What behavior did you observe? Commented May 1, 2012 at 2:30
  • I'm sorry! :P 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 <code>document.getElementById("display").innerHTML="hello!";</code>, but isn't now. Thanks! Commented May 1, 2012 at 2:32

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

And you can do it like this:

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

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.