EDIT: Thanks Kenny and everyone else who answered similarly. It really was a DOH...So stupid of me not to remember this.
Maybe the answer is so simple it's eluding me...but I know someone here can school me on this hopefully.
So I've got a rather large project that's dealing with tons of very large JSON, Objects, Arrays, etc..And I need a way of dynamically accessing this data without prior knowledge of the actual names of these. I know someobjectname[string] works, but I need [string][string][string] etc etc. In other words, the entire thing has to be completely dynamic.
I know, I know, there's performance issues with this approach and I'm sure there is better methods but I'm inheriting this problem and believe me, it's not an option to change it.
NOW, here's a super-uber over simplified example to prove the underlying problem. I can't find a way without using eval(), which I can't use because the data is NOT coming trusted sources.
In this example, pretend that foo and bar (both the object names and corresponding option values) are NOT able to known until runtime. Let's say for simplicity they're printed w/ your favorite server-side code.
<script>
// Pretend these objects are inserted into
// the DOM dynamically from where ever
// so we don't know the names till runtime
var foo = {
value : "something"
}
var bar = {
value : "something else"
}
window.onload = function() {
function alertValue(option) {
// vvvv This is what I can't do
var selected_object = eval(option.getAttribute("value"));
var selected_value = selected_object.value;
alert(selected_value);
}
var option1 = document.getElementById("option1");
var option2 = document.getElementById("option2");
option1.onclick = function () {
alertValue(this);
}
option2.onclick = function () {
alertValue(this);
}
}
</script>
<html>
<select> <!-- Pretend these values are generated at runtime serverside -->
<option id="option1" value="foo">Foo's value</option>
<option id="option2" value="bar">Bar's value</option>
</select>
</html>
Any help would be great. I'm hoping it's a simple "DOH" moment. Please don't rip apart this code or the method cause there's no point. It isn't anywhere near as complex as the real project is. It's just a proof of concept so you get the problem.