By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,583 Members | 617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,583 IT Pros & Developers. It's quick & easy.

How to access field name or ID of current element?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a

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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.