SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to click on a table cell (checkbox)

    I am trying to loop through all rows of a table object and click on the first cell of each row (which happens to be a checkbox).

    This is the javascript I have got so far (does not work)

    function selectAll(tableName) {
    table = document.getElementById(tableName);
    var rows = table.rows;
    for (i=1; i<rows.length; i++) {
    var cells = rows[i].cells;
    element = cells[1];
    element.click();
    }

    }

    When I use this function I get the following error
    Error: element.click is not a function

    How do I do this? Please help.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you trying to mark each checkbox as checked, or are you trying to call onclick functions or something for each one? If you just need them all checked, give all your checkboxes the attribute name="mycheckbox" and call:

    var checkboxes = document.getElementsByName("mycheckbox");
    for( var i=0; i < checkboxes.length; i++ ) {
    checkboxes[i].checked = true;
    }

  3. #3
    SitePoint Evangelist comfixit's Avatar
    Join Date
    Dec 2004
    Location
    Pasadena
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Checkboxes are form elements so perhaps you need the form name somewhere in your DOM tree to properly address the check box elements.

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser
    Are you trying to mark each checkbox as checked, or are you trying to call onclick functions or something for each one? If you just need them all checked, give all your checkboxes the attribute name="mycheckbox" and call:

    var checkboxes = document.getElementsByName("mycheckbox");
    for( var i=0; i < checkboxes.length; i++ ) {
    checkboxes[i].checked = true;
    }
    If we use the same name for a group of checkboxes, then how do we read the individual values of these checkboxes upon POST? In the servlet I am using request.getParameter(checkboxname) to get the value of the checked checkboxes.

    wouldn't it be a problem if all checkboxes have the same name?

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really, if you are using them to mark a specific record in a database you'd use:
    <input type="checkbox" name="id" value="1001">
    <input type="checkbox" name="id" value="1002">
    <input type="checkbox" name="id" value="1003">
    etc

    Lets say the user checks the first and last checkbox

    Then in your page that receives the post:
    request.getParameter(id)
    would = "1001,1003"
    which you can then split into an array and process

  6. #6
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser
    Not really, if you are using them to mark a specific record in a database you'd use:
    <input type="checkbox" name="id" value="1001">
    <input type="checkbox" name="id" value="1002">
    <input type="checkbox" name="id" value="1003">
    etc

    Lets say the user checks the first and last checkbox

    Then in your page that receives the post:
    request.getParameter(id)
    would = "1001,1003"
    which you can then split into an array and process
    I tried exactly this, when I call request.getParameter(id) I get only 1001 even though 1003 was also selected.

    What am I doing wrong?

    here is my code:

    <html>
    <body>
    <form method="POST" action="../servlet/TestServlet" name="form1">
    <input type="checkbox" name="id" value="1001">1001<br>
    <input type="checkbox" name="id" value="1002">1002<br>
    <input type="checkbox" name="id" value="1003">1003<br>
    <input type=Submit>
    </form>
    </body>
    </html>

    public class TestServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String value = req.getParameter("id");
    logger.info("value = " + value);
    }


    }

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my java sucks but try:

    Array aCheckbox = req.getParameterValues("id");

  8. #8
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser
    my java sucks but try:

    Array aCheckbox = req.getParameterValues("id");
    That works
    Thanx a lot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •