Solved

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

Posted on 2003-12-04
4
2,913 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript Hashmap output 8 44
Why does my button disappear? 3 26
JavaScript: Issue with onClick 5 34
Jquery get each value 2 0
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now