Solved

convert string to object

Posted on 2000-04-12
11
436 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
ID: 2709079
var theInputObj = eval("document.form1.input1");
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2709084
... then you can do this:

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

etc
0
 

Author Comment

by:siouxRules
ID: 2709205
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
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.

 

Author Comment

by:siouxRules
ID: 2709214
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
ID: 2709275
Can you show me the code you are using, including the HTML for the FORM and in INPUT?  Thanks
0
 

Author Comment

by:siouxRules
ID: 2709320
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
ID: 2709335
"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
ID: 2709342
... p.s.  use parseFloat if the value may contain decimals.
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 2709353
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
ID: 2709417
.. 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
ID: 2709539
thank you

su
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

815 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

8 Experts available now in Live!

Get 1:1 Help Now