checkbox (I guess any input field really) arrays - how to identify the "current" entry?

Discussion in 'Javascript' started by Jeremy, Oct 2, 2006.

  1. Jeremy

    Jeremy Guest

    If we have a table with say 3 rows in it and one of the columns contains
    a checkbox such as

    <table>
    <tr>
    <td><input type="checkbox" name="ch_fld"></td>
    </tr>
    <tr>
    <td><input type="checkbox" name="ch_fld"></td>
    </tr>
    <tr>
    <td><input type="checkbox" name="ch_fld"></td>
    </tr>
    </table>

    What code can I add to an "onclick" in order to determine which
    occurrence of the checkbox has just been clicked?


    --
    jeremy
     
    Jeremy, Oct 2, 2006
    #1
    1. Advertising

  2. Jeremy

    Matt Kruse Guest

    Matt Kruse, Oct 2, 2006
    #2
    1. Advertising

  3. Jeremy

    Jeremy Guest

    In article <>, Matt Kruse says...
    > Jeremy wrote:
    > > What code can I add to an "onclick" in order to determine which
    > > occurrence of the checkbox has just been clicked?

    >
    > <input type="checkbox" name="ch_fld" onclick="callfunc(this)">
    >
    > Now your callfunc function will be passed a reference to the checkbox that
    > has been clicked.
    > http://www.javascripttoolbox.com/bestpractices/#forms
    >


    Perhaps I dodn't make the question quite clear - I am seeking to know
    the "index" of the item clicked - is it 0,1 or 2 (pertaining to my
    suggested example in which there were 3 entries of the same name)?

    --

    jeremy
     
    Jeremy, Oct 2, 2006
    #3
  4. Jeremy

    Jeremy Guest

    In article <>, Ed Jay says...
    > Jeremy scribed:
    >
    > >In article <>, Matt Kruse says...
    > >> Jeremy wrote:
    > >> > What code can I add to an "onclick" in order to determine which
    > >> > occurrence of the checkbox has just been clicked?
    > >>
    > >> <input type="checkbox" name="ch_fld" onclick="callfunc(this)">
    > >>
    > >> Now your callfunc function will be passed a reference to the checkbox that
    > >> has been clicked.
    > >> http://www.javascripttoolbox.com/bestpractices/#forms
    > >>

    > >
    > >Perhaps I dodn't make the question quite clear - I am seeking to know
    > >the "index" of the item clicked - is it 0,1 or 2 (pertaining to my
    > >suggested example in which there were 3 entries of the same name)?

    >
    > Perhaps this will work?
    >
    > function getIndex (what) {
    > var indexValue = 0;
    > for(var i=0; i < what.length; i++) {
    > if(what.checked) {indexValue = i;}
    > }
    > return indexVal; //Or do what you want with indexValue here.
    > }
    >

    Now, if I am reading that right, that will return me every occurrence
    that has been checked - what I would like to know is which occurrence
    did the user just click?

    --

    jeremy
     
    Jeremy, Oct 2, 2006
    #4
  5. Jeremy

    Matt Kruse Guest

    Ed Jay wrote:
    > function getIndex (what) {
    > var indexValue = 0;
    > for(var i=0; i < what.length; i++) {
    > if(what.checked) {indexValue = i;}
    > }
    > return indexVal; //Or do what you want with indexValue here.
    > }


    Or:

    function getIndex(obj) {
    var indexValue = 0;
    var els = obj.form.elements[obj.name];
    for(var i=0; i < els.length; i++) {
    if(els==obj) {return i;}
    }
    return -1;
    }
    (untested)

    You would want to add some error-checking in there in case there is only 1
    checkbox in the group, for example.

    Also, you may want to consider the need to finding the index to begin with.
    There may be a better way to accomplish what you are ultimately trying to
    do.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Oct 2, 2006
    #5
  6. Jeremy

    Jeremy Guest

    In article <>, Matt Kruse says...
    > Also, you may want to consider the need to finding the index to begin with.
    > There may be a better way to accomplish what you are ultimately trying to
    > do.
    >


    Thanks for your help with this. The need is to be able to have a grid
    (table) of variable length with a variable number of fields in it and,
    upon submission of the form, be able to identify those "rows" where the
    data has changed. The problem encountered pertained to checkboxes, which
    are only submitted when they are checked and hence when a checkbox was
    checked/unchecked, wanted to set a corresponding hidden field [indexed]
    to a Y or N value.

    --

    jeremy
     
    Jeremy, Oct 5, 2006
    #6
  7. Jeremy

    Matt Kruse Guest

    Jeremy wrote:
    > when a checkbox was checked/unchecked, wanted to set a corresponding
    > hidden field [indexed] to a Y or N value.


    Instead, you could keep the hidden field in the same table cell.
    When the checkbox is clicked, find the TD containing it, then find the
    hidden field within the TD with the name you want, and use it. There is no
    need to go up to the form level and try to find things that way.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Oct 5, 2006
    #7
  8. Jeremy

    Jeremy Guest

    In article <>, Matt Kruse says...
    > Jeremy wrote:
    > > when a checkbox was checked/unchecked, wanted to set a corresponding
    > > hidden field [indexed] to a Y or N value.

    >
    > Instead, you could keep the hidden field in the same table cell.
    > When the checkbox is clicked, find the TD containing it, then find the
    > hidden field within the TD with the name you want, and use it. There is no
    > need to go up to the form level and try to find things that way.
    >


    OK thanks I may look into that

    --
    jeremy
     
    Jeremy, Oct 5, 2006
    #8
    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. AtomicBob
    Replies:
    14
    Views:
    966
    Toby Inkster
    May 2, 2006
  2. Weng Tianxiang
    Replies:
    63
    Views:
    2,110
    Weng Tianxiang
    Sep 26, 2007
  3. Guest
    Replies:
    0
    Views:
    586
    Guest
    Sep 17, 2007
  4. Big RaRa
    Replies:
    0
    Views:
    433
    Big RaRa
    Dec 1, 2007
  5. Joey Pang
    Replies:
    4
    Views:
    563
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page