How to access field name or ID of current element?

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

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

    Thanks in advance.
    , Sep 12, 2006
    #1
    1. Advertising

  2. Evertjan. Guest

    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., Sep 12, 2006
    #2
    1. Advertising

  3. RobG Guest

    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
    RobG, Sep 12, 2006
    #3
  4. 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.
    , Sep 12, 2006
    #4
  5. 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.
    , Sep 12, 2006
    #5
  6. Evertjan. Guest

    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)
    Evertjan., Sep 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Davis
    Replies:
    2
    Views:
    384
    Dave Anderson
    Aug 18, 2003
  2. Matthew Heidemann
    Replies:
    14
    Views:
    245
    eT Ma
    Mar 22, 2011
  3. M Wells
    Replies:
    4
    Views:
    97
    Bas Cost Budde
    Feb 3, 2004
  4. Pavils Jurjans
    Replies:
    3
    Views:
    181
    Pavils Jurjans
    Mar 18, 2005
  5. Replies:
    6
    Views:
    166
Loading...

Share This Page