Solved

convert string to object

Posted on 2000-04-12
11
437 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

839 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