views:

96

answers:

5

Let's say I have a function which is being passed a string which originally came from getElementById, and I have an object with the same name as that string's value, is there a way to call that object? I won't know which object I want until I get that value from the element's ID.

For Instance:

StartingFunction(SomeID){
someVariable = document.getElementById(SomeID).id
somefuntion(someVariable)
}

someFunction(ElementID){
// need to call Object.Value of whichever object is named the same as the value of   
//ElementID here

}

ElementID.Value obviously won't work since ElementID is just a variable, not an object...

A: 

What you call ElementID is actually the element itself because you are passing document.getElementById() into somefunction.

Jeremy Neiman
I believe in my function, I am actually passing getElementById(SomeID).id I will edit.
Chris Sobolewski
If you already know the id why are you getting it again? document.getElementById(someID).id is equivalent to someID.
Jeremy Neiman
This is an oversimplified version of what I'm actually doing and... well, I'm very green. I've been using alerts to make sure that my code works and alert(SomeID) alerts "[HTML div element]" or something like that, so I opted for safety. If this is an unneeded concern, that will actually make my code a lot easier to handle...
Chris Sobolewski
Rather than alerting out DOM elements. Why not use Firefox and FireBug. Then you can use `console.log(element)` and get detailed information on the DOM element in the Firebug console. Obviously only use this logging for development as it requires Firefox and FireBug
Karl
+1  A: 

if function is in global scope you can just window[ElementID] for example:

   someFunction(ElementID){
        return window[ElementID].value;

    }
Eldar Djafarov
A: 

You can pass the element directly to someFunction.

For example:

StartingFunction(SomeID){
  var element = document.getElementById(SomeID);
  somefuntion(element);
}

someFunction(element){
  alert(element.id);
  alert(element.value);
  // Any other processing you want to do with element
}

Or if you need to be able to get an element from an id just use getElementById

someFunction(id) {
  var element = document.getElementById(id);
  alert(element.value);
  // Any other processing you need to do with the element DOM object
}
Karl
SomeID isn't global...
Chris Sobolewski
that should not be a problem
Karl
+1  A: 

Don't do that. This is bad design and will lead to enormous pain and hard-to-find bugs down the road.

Instead, use a global object that has all of the objects you want to reference.

var valueMap = new Object();

function setValue(id, valueObject) {
    valueMap[id] = valueObject;
}

function someFunction(id) {
    return valueMap[id].Value;
}
JSBangs
There is already a global object, called window. But I agree that using that is a little hacky.
Mr. Shiny and New
A: 

This makes no sense:

someVariable = document.getElementById(SomeID).id

You are fetching the id of the element with id SomeID...why not just use SomeID?

If you want the value attribute of the object with id SomeID, just do:

document.getElementById(SomeID).value
truppo
Or just pass a reference to the element directly:StartingFunction(SomeID){element = document.getElementById(SomeID)somefuntion(element)}someFunction(element){var x = element.Value}
Karl
I anwered above. It's mostly me being new at this and me worrying too much about my alert()s that I use to make sure things are going as planned.alert(ElementId) returning me "[HTML Div Element]" or whatever made me worry that code wouldn't work unless I got it to alert the string I wanted it to.Is this incorrect?
Chris Sobolewski
"Or just pass a reference to the element directly: StartingFunction(SomeID){ element = document.getElementById(SomeID) somefuntion(element) } someFunction(element){ var x = element.Value } – Karl 12 mins ago"So you're telling mean element that alerts "[HTML Div Element]" with an ID of "ID" or whatever is the same as var ID = new Object()? And that element.value is the same as ID.value? If so... that would be really cool and make things a lot easier, but it seemed to good to be true.
Chris Sobolewski
Nope. An element with an id of "ID" is NOT the same as var ID = new Object()
Karl
What I am saying is just pass the element directly as a parameter to someFunction.
Karl
Oh, well that's easy. The element is an icon style picture. It does not and could not contain nearly the ammount of data that is stored with it. I use that technique in a number of spots, but now I need to access data that is not contained within the element, but still needs to be associated with it. I could name it Fu for all the difference it would make, but for my sanity's sake it makes more sense to name it the same thing as the element, which are both descriptive names that tell me exactly what we're talking about.
Chris Sobolewski