How to access field name or ID of current element?

Discussion in 'Javascript' started by effendi@epitome.com.sg, Sep 12, 2006.

Guest

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.

Advertisements

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)

Advertisements

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:

Guest

RobG wrote:
> 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.

Guest

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.

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!