Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to store references to objects in an array

Posted on 2004-11-26
9
Medium Priority
?
188 Views
Last Modified: 2010-05-19
Hi
My aim is to have an array as a property of an object, in which references to html objects are stored.
Here is a sample code of what I mean:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script>
obj = {}
obj.div = document.getElementById("mydiv")
function one(){
      document.getElementById("mydiv").style.visibility="hidden"
}
function two(){
      obj.div.style.visibility="hidden"
}
</script>
<body>
<div id="mydiv">abcde</div>
<a href="javascript:void(0)" onClick="one()">One</a>
<a href="javascript:void(0)" onClick="two()">Two</a>
</body>
</html>

The "one" link works, but the "two" link fails giving "object exptected" error. typeof(obj.div) gives me null, while I want this to give me object.
Am I misunderstanding any thing here?
Huji
0
Comment
Question by:huji
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:archrajan
ID: 12683083
u have just one div and that works when u call that...
and when the function executes its style is set to hidden and there is nothing available for the second one to execute
0
 
LVL 25

Expert Comment

by:archrajan
ID: 12683085
can u give a more clear explanation of what u r trying to do here please
0
 
LVL 25

Expert Comment

by:archrajan
ID: 12683097
check this
<span id="blah"></span>
<span id="blah2"></span>
<script>
var names = new array("blah", "blah2");
var values = new array("test", "test2");
var fields = new array("innerHTML", "innerHTML");
document.getElementById(names[0])[fields[0]] = values[0];
</script>

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 25

Expert Comment

by:archrajan
ID: 12683100
also check this forum post here http://www.webmasterworld.com/forum91/2394.htm
0
 
LVL 14

Author Comment

by:huji
ID: 12683104
Again: When you click on the link "one" it works, as it should. But when you click on link "two" it fails.
I want test.div to work just as document.getElementById("mydiv")
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12683106
At the tim eof assign the div has not been parsed so it does not exist, putting the script after the div (or calling off the onload) will solve this

<body>
<div id="mydiv">
<p>abcde</p>
</div><a href="javascript:void(0)" onClick="one()">

<p>One</a> <a href="javascript:void(0)" onClick="two()">Two</a> <script>
obj = {}
obj.div = document.getElementById("mydiv")
function one(){
     document.getElementById("mydiv").style.visibility="hidden"
}
function two(){
     obj.div.style.visibility="hidden"
}
</script>
</body>

0
 
LVL 25

Expert Comment

by:archrajan
ID: 12683111

document.getElementById(ids[a])[fields[b]] = values[c]
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 12683117
shld read
 " At the time of assignment  the div.."
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 2000 total points
ID: 12683136
sorry my indentation was terrible, more tidy

<body>
 <div id="mydiv">
  <p>abcde</p>
 </div>
  <a href="javascript:void(0)" onClick="one()"><p>One</a>
  <a href="javascript:void(0)" onClick="two()">Two</a>

<script>
 obj = {}
 obj.div = document.getElementById("mydiv")
 function one(){
     document.getElementById("mydiv").style.visibility="hidden"
 }
 function two(){
     obj.div.style.visibility="hidden"
 }
</script>
</body>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 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