• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

convert string to object

in jscript, how would i convert a string that contain object like "document.form1.input1.value" to the actual object itself.



0
siouxRules
Asked:
siouxRules
  • 7
  • 4
1 Solution
 
knightEknightCommented:
var theInputObj = eval("document.form1.input1");
0
 
knightEknightCommented:
... then you can do this:

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

etc
0
 
siouxRulesAuthor Commented:
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
siouxRulesAuthor Commented:
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
 
knightEknightCommented:
Can you show me the code you are using, including the HTML for the FORM and in INPUT?  Thanks
0
 
siouxRulesAuthor Commented:
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
 
knightEknightCommented:
"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
 
knightEknightCommented:
... p.s.  use parseFloat if the value may contain decimals.
0
 
knightEknightCommented:
Let's just simplify the whole thing:

for(i=1; i<=10; i++)
{
   total += parseInt( eval("document.form1.child"+i+"Quan.value"), 10 );
}
0
 
knightEknightCommented:
.. 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
 
siouxRulesAuthor Commented:
thank you

su
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now