Solved

convert string to object

Posted on 2000-04-12
11
438 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
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.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

730 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