Solved

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

Posted on 2010-11-22
5
381 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

719 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