Solved

convert string to object

Posted on 2000-04-12
11
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

695 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