0

Please help me whats wrong code ?

function runPrompt(Message, promptLocation, color)
{
document.getElementById("promptLocation").innerHTML = Message;
document.getElementById("promptLocation").style.color = color;
}

Name : 
<input id= "commandName" onkeyup = "validateName()" type = "text"> <label id = "namePrompt"></label>

2 Answers2

1

It's unclear what you are after without posting your html. I think you are trying to use promptLocation parameter as the id of the element to change. The parameter variable should be passed directly into getElementById and shouldn't be in quotes:

<div id="namePrompt"></div>

function runPrompt(Message, promptLocation, color)
{
    document.getElementById(promptLocation).innerHTML = Message;
    document.getElementById(promptLocation).style.color = color;
}

runPrompt('Test Message', 'namePrompt', 'red');

http://jsfiddle.net/jq8ghxkb/1/

Wayne Ellery
  • 7,758
  • 1
  • 28
  • 44
0

For safer side we need to check whether element is exist or not while using 'document.getElementById'. Because we can't access the attributes of undefined/no rendered elements in JavaScript.

In case of JQuery, $('#id') will give empty array if id is not exist.