Solved

I have the object name - how do I get the object

Posted on 2003-12-04
4
2,914 Views
Last Modified: 2007-12-19
I have the name of the object that I want (and I have it stored as a string). How do I get the object that it refers to?

eg I have the string 'document.all.clResult1.style.visibility'
how do I get hold of the object so I can set one of it's properties?

My actual problem is, I need to call document.all.clResultXX.style.visibility = "visible"; - where XX is any number at all that is passed into the function. I've tried document.all.clResult+ XX +.style.visibility = "visible"; but that gives me a javascript error.

Is there a 'getObjectByName' funtion or anything like that?
0
Comment
Question by:mailBoy2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:mrh30
ID: 9874154
Providing your element has been correctly given an 'id', not a 'name', you can use document.getElementById('...') to fetch the element.
0
 
LVL 10

Accepted Solution

by:
Nushi earned 50 total points
ID: 9874166
hi there.

i missing some information.
there is a method getElementsByName(...) => return array of all elements with this name.

in your case it should be getElementsByNames( 'clResult1')[0].style.visibility.propertyName
or use document.getElementById('id') which return the Object.

do:
document.getElementById( clResult+XX).style.visibility

but you should have id to teh field.

if its in a form you can use:
document.form_name[clResult+XX].style.visibility

Nushi.

0
 
LVL 10

Expert Comment

by:Nushi
ID: 9874176
ot be more specific:

document.all => Ie syntax.
document.getElementById => DOM - return object
document.getElementsByName => DOM - return array of all objects with this name

Nushi.
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9874203
document.getElementById('name') will work with names also but is ns6 and ie5+ only

you can also use (if you have a form on the page) which is ns4+ and ie5+ compatible

document.forms[0].elements['name']

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

785 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