Solved

convert string to object

Posted on 2000-04-12
11
434 Views
Last Modified: 2012-05-04
in jscript, how would i convert a string that contain object like "document.form1.input1.value" to the actual object itself.



0
Comment
Question by:siouxRules
  • 7
  • 4
11 Comments
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
var theInputObj = eval("document.form1.input1");
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
... then you can do this:

alert( theInputObj.name );
alert( theInputObj.value );

etc
0
 

Author Comment

by:siouxRules
Comment Utility
thanks, but i tried it and it didn't work.  when i pass the string to the eval function it return an object but when i do

theInputObj.value

it's undefine.  


0
 

Author Comment

by:siouxRules
Comment Utility
thanks, but i tried it and it didn't work.  when i pass the string to the eval function it return an object but when i do

theInputObj.value

it's undefine.  


0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
Can you show me the code you are using, including the HTML for the FORM and in INPUT?  Thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:siouxRules
Comment Utility
function calculate()
{
      var i, childString,total
      var childObject
      total = 0
      for(i= 1 ; i<=10; i++)
      {
      childString = "document.form1.child" + ("" + i) + "quan"
      childObject = eval(childString);
      total = total + childObject.value
      }
      document.form1.totalQuan.value = total;
}

this function call by submition of the form1 which has child1Quan, ..., child10quan in the form.  the form is really big, i don't think i should post it.

thank you.
 
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
"child1Quan" will be different than "child1quan" -- js is case sensitive.

Also, you should do this to calculate the total:

total += parseInt(childObject.value,10);

instead of  total = total + childObj.value   because childObj.value is a string, not a number.
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
... p.s.  use parseFloat if the value may contain decimals.
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
Comment Utility
Let's just simplify the whole thing:

for(i=1; i<=10; i++)
{
   total += parseInt( eval("document.form1.child"+i+"Quan.value"), 10 );
}
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
.. this assumes a form like this:

<FORM name='form1'>
<INPUT name='child1Quan' type='text'>
<INPUT name='child2Quan' type='text'>
<INPUT name='child3Quan' type='text'>
....
<INPUT name='child10Quan' type='text'>
</form>
0
 

Author Comment

by:siouxRules
Comment Utility
thank you

su
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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

11 Experts available now in Live!

Get 1:1 Help Now