Solved

document.form.variable.value doesnt work--  javascript

Posted on 2010-11-22
5
375 Views
Last Modified: 2012-05-10
Hi all,

I'm trying to construct a variable -> which consists of the element name..like this
str =  ".hdnaccnum"+((frm.name).substr(3));   // returns hdnaccnum15  

Now when i say --- alert(document.frmAvgData+str+".value");       It does'nt work
But when i say  ---- alert(document.frmAvgData.hdnaccnum15.value) ;   // it does work.

How can i construct dynamic variable which refers to an element and get its value
0
Comment
Question by:kishan66
  • 2
  • 2
5 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 200 total points
ID: 34190518
You can use EVAL but it is evil (use with care)

alert( eval("document.frmAvgData." + str + ".value") )
0
 

Author Comment

by:kishan66
ID: 34190529
in the above code ...
str =  ".hdnaccnum"+((frm.name).substr(3));
(frm.name).substr(3) returns the number -- expected value
0
 

Author Comment

by:kishan66
ID: 34190559
Thanks cyberkiwi.

It works!!!

But may i ask you why is it kind of Evil? what things should be taken care if i have to use "eval"

0
 
LVL 2

Assisted Solution

by:benschwartz
benschwartz earned 50 total points
ID: 34190573
You can't dynamically form property names in JavaScript, however you can treat JavaScript objects as arrays. Rather then using concatenation to generate a property name, use it to generate an associative array "key". Treat your object as an array and supply your generated key in brackets to get a handle on your property.

alert(document.["frmAvgData" + str].value);
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34190604
http://blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx

and quote from JsLint (an _excellent_ JS resource)
http://www.jslint.com/lint.html
eval is evil
The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes necessary, but in most cases it indicates the presence of extremely bad coding. The eval function is the most misused feature of JavaScript.

It is too powerful a feature, so the consensus is that if there are any other alternatives at all, exhaust them all before using eval().  It is necessary for some things though.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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.
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…

860 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