Solved

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

Posted on 2010-11-22
5
378 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
[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
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

738 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