473,386 Members | 1,830 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,386 software developers and data experts.

How to access field name or ID of current element?

I am trying to write a simple routine multiplying one value of a field
to another. i.e cost1 multiple by cost2.. But since I have many lines
of these, I want to write a function by accessing the "fieldname" and
passing it to the function in the onChange event. For example, if I am
in the cost1 field, I would like the onChange event to "retrieve" the
field name so that I can pass it to the function.Besides feld names,
each field also have a unique ID. Sorry if this is simplistic, but I
haven't come across anything that can tell me the name of the current
field.

Thanks in advance.

Sep 12 '06 #1
5 7563
wrote on 12 sep 2006 in comp.lang.javascript:
I am trying to write a simple routine multiplying one value of a field
to another. i.e cost1 multiple by cost2.. But since I have many lines
of these, I want to write a function by accessing the "fieldname" and
passing it to the function in the onChange event. For example, if I am
in the cost1 field, I would like the onChange event to "retrieve" the
field name so that I can pass it to the function.Besides feld names,
each field also have a unique ID. Sorry if this is simplistic, but I
haven't come across anything that can tell me the name of the current
field.
Use 'this':

<input onchange='yourFunction(this)'
....

function yourFunction(elmnt){
var z = elmnt.value
....

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 12 '06 #2

ef*****@epitome.com.sg wrote:
I am trying to write a simple routine multiplying one value of a field
to another. i.e cost1 multiple by cost2.. But since I have many lines
of these, I want to write a function by accessing the "fieldname" and
passing it to the function in the onChange event. For example, if I am
in the cost1 field, I would like the onChange event to "retrieve" the
field name so that I can pass it to the function.Besides feld names,
each field also have a unique ID. Sorry if this is simplistic, but I
haven't come across anything that can tell me the name of the current
field.
In a function referrenced by a DOM object's event handler, the this
operator will refer to the DOM object:

<script type="text/javascript">

function showInfo(el){
var msg = '';
if (el.id) msg += el.id;
if (el.form) {
msg += '\n' + el.nodeName + ' is in a form';
} else {
msg += '\n' + el.nodeName + ' is not a form';
}
alert(msg);
}

</script>

<button id="Fred_the_Button" onclick="showInfo(this)">Fred the
Button</button>

<form action="">
<button id="Sam_the_Button" onclick="showInfo(this)">Sam the
Button</button>
</form>
Note that the button's form property is a reference to the form that
the button is in (if it's in one).
--
Rob

Sep 12 '06 #3

RobG wrote:
ef*****@epitome.com.sg wrote:
I am trying to write a simple routine multiplying one value of a field
to another. i.e cost1 multiple by cost2.. But since I have many lines
of these, I want to write a function by accessing the "fieldname" and
passing it to the function in the onChange event. For example, if I am
in the cost1 field, I would like the onChange event to "retrieve" the
field name so that I can pass it to the function.Besides feld names,
each field also have a unique ID. Sorry if this is simplistic, but I
haven't come across anything that can tell me the name of the current
field.

In a function referrenced by a DOM object's event handler, the this
operator will refer to the DOM object:

<script type="text/javascript">

function showInfo(el){
var msg = '';
if (el.id) msg += el.id;
if (el.form) {
msg += '\n' + el.nodeName + ' is in a form';
} else {
msg += '\n' + el.nodeName + ' is not a form';
}
alert(msg);
}

</script>

<button id="Fred_the_Button" onclick="showInfo(this)">Fred the
Button</button>

<form action="">
<button id="Sam_the_Button" onclick="showInfo(this)">Sam the
Button</button>
</form>
Note that the button's form property is a reference to the form that
the button is in (if it's in one).
--
Rob
Rob

Thanks this is what I was looking for. I need the nodename so that I
can identify all the associated fields.

Sep 12 '06 #4

Evertjan. wrote:
wrote on 12 sep 2006 in comp.lang.javascript:
I am trying to write a simple routine multiplying one value of a field
to another. i.e cost1 multiple by cost2.. But since I have many lines
of these, I want to write a function by accessing the "fieldname" and
passing it to the function in the onChange event. For example, if I am
in the cost1 field, I would like the onChange event to "retrieve" the
field name so that I can pass it to the function.Besides feld names,
each field also have a unique ID. Sorry if this is simplistic, but I
haven't come across anything that can tell me the name of the current
field.

Use 'this':

<input onchange='yourFunction(this)'
...

function yourFunction(elmnt){
var z = elmnt.value
...

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan

Thanks for responding. I am trying to access the name or the id of the
field and not its value.

Sep 12 '06 #5
wrote on 12 sep 2006 in comp.lang.javascript:
>
Evertjan. wrote:
>wrote on 12 sep 2006 in comp.lang.javascript:
I am trying to write a simple routine multiplying one value of a
field to another. i.e cost1 multiple by cost2.. But since I have
many lines of these, I want to write a function by accessing the
"fieldname" and passing it to the function in the onChange event.
For example, if I am in the cost1 field, I would like the onChange
event to "retrieve" the field name so that I can pass it to the
function.Besides feld names, each field also have a unique ID.
Sorry if this is simplistic, but I haven't come across anything
that can tell me the name of the current field.

Use 'this':

<input onchange='yourFunction(this)'
...

function yourFunction(elmnt){
var z = elmnt.value
...

Thanks for responding. I am trying to access the name or the id of the
field and not its value.
Same thing:

function yourFunction(elmnt){
var theID = elmnt.id;
var theName = elmnt.name;
....

However, having the element as an object, why would you want the id?

var theID = elmnt.id;
var myElement = document.getElementById(theID);

seems only a complicated way of saying:

var myElement = elmnt;
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 12 '06 #6

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

Similar topics

8
by: Calan | last post by:
I have a server-side ASP script that dynamically creates an input form from a database table. The table contains a field name, the table where values are stored, type of input control, value for a...
6
by: charlie_M | last post by:
I figured out via various help from this forum... EXAMPLE: onClick="document.forms.MYBUTTON.value='SIMPLE';document.forms.submit()" In my CGI I see "MYBUTTON" = "SIMPLE" and this works...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
6
by: F-13 | last post by:
I'm working on a BOM in Access 200 from an example downloaded from from the web. The sample database contains three tables, Assemblies (the list of items needed to assemble any assembly),...
8
by: Martin Marcher | last post by:
Hi, I'm working on a program that creates a linear list of structs struct lin_list{ struct lin_list *next; char Name; }; this is what it looks like. And i got (design) problems with the...
6
by: HS1 | last post by:
Hello I have a table in Access Database. This table has a AutoNumber field. I use a DataGrid to show that table When I insert a new record in for this table using a DataGrid, there is a...
13
by: mfreeman | last post by:
The minimal code (VB.NET 2003) needed to show this problem is shown below. All I do is loop through the records in the table and update them without making any changes. Out of 600 records, about...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
1
MMcCarthy
by: MMcCarthy | last post by:
Access has a number of built-in functions which can be generally used in queries or VBA code. Some of the more common ones are: Note: anything in square brackets is optional Date Functions ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.