Hi I am fairly new to Javascript.
I am trying to pass a parameter into a function and have that function generate a variable name that contains the passed parameter.
As you can see below I am trying to generate a variable name from “Q” + ActiveQuestionHolder + “Answer” with a value of document.querySelector(QuestionMenu).value;
In console I am coming up with “SyntaxError: unexpected token: string literal”.
I realize there maybe a better way but I am unsure what that maybe.
function QChoiceFunction(ActiveQuestionHolder) {
QuestionMenu = "#Q" + ActiveQuestionHolder +"Choices";
QuestionClassAnswer = ".Q"+ ActiveQuestionHolder +".Answer";
let "Q" + ActiveQuestionHolder + "Answer" = document.querySelector(QuestionMenu).value; // Takes value from selected dropdown ID and assigns to variable -- Getting element content
console.log(QAnswer);
document.querySelector(QuestionClassAnswer).textContent = QAnswer; // Puts the Value into the div with the proper class.
//QuestionResponse = "Q" + ActiveQuestionHolder +"Answer";
//QAnswer = QuestionResponse;
console.log("Q1Answer is = " + Q1Answer);
//console.log("QuestionResponse is = " + QuestionResponse);
}
QChoiceFunction(1);
I am trying to create variables Q1Answer, Q2Answer, Q3Answer etc based on the parameter passed into the function.
So far I am able to create the variable value with = document.querySelector(QuestionMenu).value; which is pulling data from a drop-down menu.
let "Q" + ActiveQuestionHolder + "Answer" = document.querySelector(QuestionMenu).valueWhat does this mean ? You are attempting to assign a primitive value to a operation ? This doesn't mean anything syntaxically, and this is why you are having a syntax error.