I am currently writing code to populate a select tag with all the hours of the day. I recently rewrote a function that will populate a temporary select tag based off a time passed in as an argument and then return the temporary select at the end. When I attempt to assign the product to a variable, Javascript gives the error "Invalid Assignment on left-hand side." Have I stumbled onto one of the cases where you can't directly assign variables to each other, or is this a syntax error?

This the line that is giving me an error

eval(select1) = newHours('000');

Where select1 is a variable that contains the name of the day and a number, both of which change depending on input, hence the eval statement. The function newHours returns a temporary select.

Recommended Answers

All 6 Replies

This is a syntax error.
Why don't you use a hashtable or an array to hold values related to "the name of the day and a number"?
I don't really understand what you are trying to do, so I can't really give you any sample code.

I'm trying to write a function that will create a select, populate it with the hours of a the day, then return that select. I then want to assign that result to a variable, which I will add to the page.

I'm not sure how a hash table would help. The variable select1 names the selects based off the day and the number of selects present in a given div tag. Thus, the 3rd select in the Monday tag would be Monday3. That way, all the selects have a distinct name that can be used to access the selects later.

This the line that is giving me an error

eval(select1) = newHours('000');

Are you trying to assign whatever is returned by newHours() to a bunch of variables with names like Mon5, Tue10, Mon8, ... and so on? And is your problem that the names are [in effect] typed by the user?

To the first part, yes. The names are determined by what day the select is in and the number of selects present. That way, I can add a select and have it automatically be named to the next number ie: I have Wednesday1 and Wednesday2 so the next select to be made will be Wednesday3. The problem is that, for some reason, the posted code is incorrect and throws an error. I don't know enough about Javascript to see what is wrong.

Code pertaining to the selects

var select1 = day + "1";
var select2 = day + "2";
eval("var " + select1 + "= document.createElement('select');");
eval("var " + select2 + "= document.createElement('select');");

The eval statements cause the select1 and 2 to be read as the day that is passed in and the number. In this case, these are the first two selects to be assigned so hard coding in 1 and 2 is not a problem. The first line will, after being evaluated, read var Monday1 = document.createElement('select'); (assuming that the day variable holds "Monday")

I have discovered the correct way of assigning the selects, if anyone else was having the same trouble.

eval(select1 + " = newHours('000');");

You use strings determined at runtime to name variables.
A hashtable is structure that can store data based on a key.
Strings are often used as keys.

var data = {};
var select1 = "wed3";

data["Monday1"] = newHours("000");
data[select1] = newHours("000");

var monday1 = data["Monday1"];
var value = data[select1];

This way you can avoid all the eval calls and namespace pollution with variable names that you never explicitly declared.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.