0

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?

asked Mar 17, 2016 at 2:06
3
  • Function parameters are variables (not string literals). Have you ever written any JS code before? Commented Mar 17, 2016 at 2:10
  • 1
    If you are not familiar with functions, read a tutorial first: eloquentjavascript.net/03_functions.html. This will provide much more information than one could provide in an answer on Stack Overflow. Commented Mar 17, 2016 at 2:16
  • 1
    it will not validate when tested. Do you mean that it results in a syntax error? In that case, the error will report a line number where the error occurred. It probably will give an error such as "SyntaxError; unexpected string" on the line containing 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. Commented Mar 17, 2016 at 4:23

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.