Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

771 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

12 Experts available now in Live!

Get 1:1 Help Now