0

I often use functions like this one:

function elem(name){
 return document.getElementById(name);
}

Which is used like so:

var box = elem('box');

Often, the name of the variable I'm creating is the same as the string passed to the elem function.

How can I create a function that returns an element with the same id as the variable name?

asked Jun 22, 2015 at 11:07
1
  • Do you realize you're re-inventing the wheel with that function, right ? That said there's nothing I know to do what you wish, maybe you can just drop the variable and use the function call instead ? Commented Jun 22, 2015 at 11:11

4 Answers 4

3

The short answer is that you can't. The function has no way to know what the variable name is.

The closest you could come would be to use a horrible, horrible hack and create a global:

function box (element_id) {
 window[element_id] = document.getElementById(element_id);
}
box('foo');

... but even that does the opposite of what you've asked for (creating the variable name from the string instead of the other way around).

answered Jun 22, 2015 at 11:10
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. I guess I'll test it and play around a bit.
1
function elem(name){
 window[name]=document.getElementById(name);
}

Now, elem('box'); will create global variable box = document.getElementById('box'); which you can use.

Sample Code

<!DOCTYPE html>
<html>
<body>
 <div id="box" onclick="hi();">box</div> 
<script type='text/javascript'>
 function elem(name){
 window[name]=document.getElementById(name);
 }
 function hi(){
 elem('box');
 box.style.border='1px solid #ccc';
 }
</script>
</body>
</html>
answered Jun 22, 2015 at 11:21

Comments

0

better make a namespace like

var ns = {};
['box', 'box2'].forEach(function (el) { ns[el] = document.getElementById(el); });

access with

ns.box.innerHtml = 'foo';
answered Jun 22, 2015 at 11:16

1 Comment

I'd rather it not use an object. It does make sense, just I still want it as an independent variable name.
0

You can always use the DOM methods available without needing to generate a variable from the element ID. Example, instead of using

var elem = document.getElementById("box");
elem.innerHTML = "Test";

you can directly use:

box.innerHTML = "Test";

But if you really need a variable from the same name for other reasons, I think can be done is using the eval() function like so:

function box(elem){
 eval("var "+ elem + " = document.getElementById('" + elem+ "');");
}
answered Jun 22, 2015 at 11:46

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.