473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

String to Object convertion

1
Hi all,

i have got:

(syntax only to visualize)

Expand|Select|Wrap|Line Numbers
  1. elem = elem // object reference of elem
  2. a = 'parentNode.parentNode'; //manual build string
  3.  
What i want to achieve:

Expand|Select|Wrap|Line Numbers
  1. 'elem.a.getAttribute('xxx');'
where 'a' should be recognized as a object pointer - and not as the string, it is original defined.

is there any way of doing this (hopefully without the use of 'eval') ?

Thanks in advance for any hints!
greets
felic
Nov 5 '06 #1
4 1103
gits
5,390 Expert Mod 4TB
hi ...

we may use something like the following example:

Expand|Select|Wrap|Line Numbers
  1. var inp = document.getElementsByTagName('input')[0];
  2. var str = 'parentNode.parentNode';
  3.  
  4. function get_node(base_node, str) {
  5.     var nav  = str.split('.');
  6.     var node = base_node;
  7.  
  8.     for (var i in nav) {
  9.         node = node[nav[i]];
  10.     }
  11.  
  12.     return node;
  13. }
  14.  
  15. alert(get_node(inp, str).tagName);
  16.  
just tested with the google-homepage in firebug-console. a property of an object might be referenced by .property_name or with the array-reference-notation ['property_name'] and we could make use of this here again :)

kind regards
Apr 8 '08 #2
rnd me
427 Expert 256MB
there is no cross borswer way to do (exactly) what you want.

this works in firefox to give every node an 'a' property, an object ref to the grandparent node
Expand|Select|Wrap|Line Numbers
  1. Object.prototype.__defineGetter__("a", function () {
  2. if(this.parentNode && this.parentNode.parentNode){
  3. return this.parentNode.parentNode;}});
this would let you code like you want (i think)
Apr 8 '08 #3
rnd me
427 Expert 256MB
Hi all,

i have got:

(syntax only to visualize)

Expand|Select|Wrap|Line Numbers
  1. elem = elem // object reference of elem
  2. a = 'parentNode.parentNode'; //manual build string
  3.  
What i want to achieve:

Expand|Select|Wrap|Line Numbers
  1. 'elem.a.getAttribute('xxx');'
where 'a' should be recognized as a object pointer - and not as the string, it is original defined.

is there any way of doing this (hopefully without the use of 'eval') ?

Thanks in advance for any hints!
greets
felic

actually, i think what you might be asking is how to use strings to refer to objects:

change it slightly to

Expand|Select|Wrap|Line Numbers
  1. a = 'parentNode.parentNode'; //manual build string
  2.  
  3. elem[a].getAttribute('xxx');
you can use eval or a function to delay the evaluation of the string, adding additional quotes where (or perhaps when) you want the properties 'stringified'
Apr 9 '08 #4
gits
5,390 Expert Mod 4TB
hi rnd_me ...

could you tell me what would be wrong with the code i posted in post #2? as far as i'm aware that code does what the OP wanted without eval and/or a fixed number of properties or might be i misunderstood the question?

kind regards
Apr 9 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vidar | last post by:
I have a problem in dotNET XSL convertion object. (XMLTRANSFORM) It won't convert UTF-8 to ISO-8859-1 I use this stylesheet for konvertion: <?xml version="1.0" encoding="ISO-8859-1"?> <!--...
3
by: RC | last post by:
Just wondering if there are good reasons (performance, ?) to use one of these string conversion alternatives over the other: 1. Convert.ToString() 2. someObject.ToString() Example:...
4
by: juli | last post by:
Good afternoon! I have an error while trying to convert to DateTime ,here is the code and the error: ArrayList al = new ArrayList(); temp_str=Line.Split(' '); al.Add(temp_str); //...
8
by: juli | last post by:
I am trying to convert an object of a class which is derived from collectionbase class.It's not working: while ((line = file.ReadLine()) != null && line.IndexOf(" ")!=-1) { al.Add(line); }...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.