How to access field name or ID of current element?

E

effendi

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

Evertjan.

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

RobG

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).
 
E

effendi

RobG said:
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

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

effendi

Evertjan. said:
wrote on 12 sep 2006 in comp.lang.javascript:


Use 'this':

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

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

Evertjan

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

Evertjan.

wrote on 12 sep 2006 in comp.lang.javascript:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top