I am confused on a task I have been given:
Function: takes 2 parameters, a string and a label id and writes the string value to the text of the label
• Example:
changeLabel('mylabel1', ‘label1’);
should change the text value of
‘<label id="label1">Form Label1</label>’
to
‘<label id="label1">mylabel1</label>’
I am not sure where to put the variables which need to be passed in, here is my code but it will not validate when it is tested.
function changeLabel('mylabel1', 'label1'){
document.getElementById("label1").innerHTML = mylabel1;
}
changeLabel('mylabel1', 'label1');
Can someone please help?
1 Answer 1
You can't define function with strings as parameters. you should do it like this:
function changeLabel(text, id){
document.getElementById(id).innerHTML = text;
}
changeLabel('mylabel1', 'label1');
answered Mar 17, 2016 at 2:10
The Process
5,9533 gold badges33 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
function. You can then ask yourself: why is a string unexpected here? if you then open the JS manual and review the syntax for function definitions, you will see that parameters cannot be strings--whatever that would mean. You should then be able to proceed to figure out the problem.