?
Solved

Dynamic variable name on LEFT side of an assignment

Posted on 2010-01-05
11
Medium Priority
?
561 Views
Last Modified: 2013-11-19
For starters, I have:

     <div id="myDiv00" onclick="doSomething(this.id)">

In my script, I have:

     var myDiv00Status = "inactive";
     var myDiv01Status = "inactive";
     var myDiv02Status = "inactive";

     function doSomething(id) {
          clickedStatus = eval("id + 'Status'");
          alert(clickedStatus);
     }

I'd like my alert to show the value of myDiv00Status, not the string "myDivStatus".  Where have I gone wrong?  Really, no pointers in JavaScript? :)

The key is that I need to get and set the value of myDivStatus, based on which was clicked on the page--not simply one or the other.

Thanks!
0
Comment
Question by:TechComposer
11 Comments
 
LVL 14

Expert Comment

by:ajitha75
ID: 26181056
try  eval(id + "Status")

Thanks
Ajitha
0
 
LVL 2

Author Comment

by:TechComposer
ID: 26181227
Well, I do feel a little stupid now ;), but that only fixes part of the problem.  As I said, I need to also set the value of myDiv00Status.  Obviously, "clickedStatus = 'active'" doesn't work, as clickedStatus is a unique variable.  However, I can't seem anything along these lines to work either:

eval(idName + "Status" = "active");
0
 
LVL 2

Author Comment

by:TechComposer
ID: 26181244
Furthermore, is there a way to do what I'm trying to do and avoid using eval altogether?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:TechComposer
ID: 26181334
Well, I found that this works:

eval(idName + "Status" + " = 'large';");

But, damn, that eval crap is clunky.  There's got to be a smarter way of doing this...
0
 
LVL 14

Expert Comment

by:ajitha75
ID: 26181337
you should use

eval(id + 'Status = "active"');

Thanks
Ajitha
0
 
LVL 14

Expert Comment

by:ajitha75
ID: 26181349
we can avoid eval.. but what is the functionality you are trying to achieve...?

Thanks
Ajitha
0
 
LVL 2

Author Comment

by:TechComposer
ID: 26181382
I'd like to get/set a global var based on the name of a clicked HTML element's ID.  There could be any number of these elements...
0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 664 total points
ID: 26181515
or something this way perhaps using an object, sorry for not using the code tags


<html>
<head>
<script type="text/javascript">
var divObjs = {
"myDiv01Status":"inactive",
"myDiv02Status":"inactive"
};

function updateDivStatus(divId)
{
    alert("Before:Status of div with ID:"+divId+ " is " + divObjs[divId + "Status"]);
    divObjs[divId + "Status"] = "active";
    alert("After:Status of div with ID:"+divId+ " is " + divObjs[divId + "Status"]);
}
</script>
</head>
<body>
<div id="myDiv01" onclick="updateDivStatus(this.id)">
click 1
</div>

<div id="myDiv02" onclick="updateDivStatus(this.id)">
click 2
</div>

</body>
</html>
0
 
LVL 14

Accepted Solution

by:
ajitha75 earned 668 total points
ID: 26181672
Try this....

<div id="myDiv00" onclick="doSomething(this)" status='inactive'>1</div>

     function doSomething(id) {
            id.attributes["status"].value= 'active';
            alert(id.attributes["status"].value);
     }
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 668 total points
ID: 26184017
var myStatii = [];
myStatii["myDiv00Status"] = "inactive";
myStatii["myDiv01Status"] = "inactive";
myStatii["myDiv02Status"] = "inactive";
function doSomething(id) {
  clickedStatus = myStatii[id + 'Status'];
  alert(clickedStatus);
}
0
 
LVL 2

Author Closing Comment

by:TechComposer
ID: 31672954
All possible and useful solutions.  I also forgot window[]...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question