473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamic field name in object

Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

regards
-max
Jan 11 '08 #1
8 3548
machoq wrote:
Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

regards
-max
Are you sure you posted to the right group?
Looks more like PHP to me.

Erwin Moller
Jan 11 '08 #2
SAM
machoq a écrit :
Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)
what is this '$field' ?
it is something wrote by PHP, no ?
It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";
file 'my_example.php' :
<?
$field = 'Hello the world';
?>
<html>
<script type="text/javascript">
var record = new Object();
record.id = '1';
record.filename = '<?= $field ?>';
alert('record #'+record.id+' : '+record.filename);
</script>
</html>

do I expect

--
sm
Jan 11 '08 #3
On Jan 11, 4:35*pm, machoq <sraiz...@gmail.comwrote:
Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";
Bracket notation.

<URL: http://www.jibbering.com/faq/faq_not..._brackets.html >
Jan 11 '08 #4
VK
On Jan 11, 7:35 pm, machoq <sraiz...@gmail.comwrote:
Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";
var record = ['filename':'foobar'];

var field = 'filename';

alert(record[field]); // 'foobar'
Jan 12 '08 #5
VK
On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
var record = ['filename':'foobar'];

The `['...`]' would mean an array initializer (ArrayLiteral),
Right, a silly typo, I just was posting after some deep array
thinking :-)
Correct would be the following use of an object initializer (ObjectLiteral):

var record = {'filename': 'foobar'};

Since `filename' is an identifier, the apostrophes around it may be omitted:

var record = {filename: 'foobar'};
Correct as long as identifier obeys to the language naming rules.
var field = 'filename';
alert(record[field]); // 'foobar'

alert() is a method of Window objects and should explicitly called
so instead of relying too much on the UA's scope chain:

window.alert(record[field]);
Stealing my ideas, you... :-)

Jan 12 '08 #6
VK wrote:
On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>Since `filename' is an identifier, the apostrophes around it may be omitted:

var record = {filename: 'foobar'};

Correct as long as identifier obeys to the language naming rules.
What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jan 12 '08 #7
VK
On Jan 12, 9:06 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
VK wrote:
On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Since `filename' is an identifier, the apostrophes around it may be omitted:
var record = {filename: 'foobar'};
Correct as long as identifier obeys to the language naming rules.

What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.
'filename' is one, 'old filename' or 'new filename' or 'filename[0]'
are not. This is what I'm saying: correct (skip on quotes) but only as
long as identifier obeys to the language naming rules so a valid
literal as defined in Javascript.
Jan 12 '08 #8
VK wrote:
On Jan 12, 9:06 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>VK wrote:
>>On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Since `filename' is an identifier, the apostrophes around it may be omitted:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> var record = {filename: 'foobar'};
Correct as long as identifier obeys to the language naming rules.
What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.

'filename' is one, 'old filename' or 'new filename' or 'filename[0]'
are not. This is what I'm saying: correct (skip on quotes) but only as
long as identifier obeys to the language naming rules so a valid
literal as defined in Javascript.
There is no condition as to the correctness of my statement.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jan 12 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
5
by: jdzemke | last post by:
I am getting 'options' is null or not an object using when using a dynamic dataset with a dropdown list in an html form. I am filling-in a text field and filtering the values in the drop-down. ...
1
by: nyc10011 | last post by:
I am trying to figure out how to remove a dynamic text field from the stage that plays captions over an .flv from an XML file. I would like to find out how to remove the field before a second .flv...
12
by: andrew cooke | last post by:
Hi, This is my first attempt at new classes and dynamic python, so I am probably doing something very stupid... After reading the how-to for descriptors at...
1
by: jmartmem | last post by:
Greetings, I have a nagging problem with client-side dynamic dependent list boxes that perhaps someone can help me troubleshoot. I have a form with a series of dynamic dependent list boxes....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.