www.webdeveloper.com
Results 1 to 10 of 10

Thread: auto check all checkboxes

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    5

    auto check all checkboxes

    i need to know how to auto check all the checkboxes at specified time?
    my coding is not working. thanks in advance.
    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body onload="setInterval('getCurrentTime()', 1000);">
    <div id="time"></div>
    <form name="myform" action="" method="post">
    <b>Your Favorite Scripts & Languages</b><br>
    <input type="checkbox" name="list" value="1">Java<br>
    <input type="checkbox" name="list" value="2">Javascript<br>
    <input type="checkbox" name="list" value="3">Active Server Pages<br>
    <input type="checkbox" name="list" value="4">HTML<br>
    <input type="checkbox" name="list" value="5">SQL<br>

    <input type="button" name="CheckAll" value="Check All">
    <input type="button" name="UnCheckAll" value="Uncheck All"
    onClick="uncheckAll(document.myform.list)">
    <br>
    </form>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
    field[i].checked = true ;
    }

    function uncheckAll(field)
    {
    for (i = 0; i < field.length; i++)
    field[i].checked = false ;
    }
    function getCurrentTime()
    {
    var myDate = new Date();
    var mySecs = myDate.getSeconds();
    var curHour = myDate.getHours();
    var curMin = myDate.getMinutes();
    var suffix = "AM";

    if(mySecs < 10)
    mySecs = "0" + mySecs;

    if(curMin < 10)
    curMin = "0" + curMin;

    if(curHour == 12 && curMin >= 1)
    {
    suffix = "PM";
    }
    if(curHour == 24 && curMin >= 1)
    {
    curHour-= 12;
    suffix = "AM";
    }
    if(curHour > 12)
    {
    curHour-= 12;
    suffix = "PM";
    }

    var time = curHour + ":" + curMin + ":" + mySecs + " " + suffix;
    document.getElementById('time').innerHTML=(time);

    if(time == "6:13:00 PM") //Change this to whatever time you want
    {
    for (i = 0; i < document.myform.list.length; i++)
    document.myform.list[i].checked = true ;
    location.reload();
    }

    }
    // End -->
    </SCRIPT>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Here's the part I think you are looking for...
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Checkbox Settings </title>
    <script type="text/javascript">
    //<![CDATA[
    function CheckAll(flag) {
      var sel = document.getElementsByTagName('input');
      for (var i=0; i<sel.length; i++) {
        if (sel[i].type == 'checkbox') { sel[i].checked = flag; }
      }
    }
    //]]>
    </script>
    
    </head>
    <body>
    <input type="checkbox" name="list" value="1">Java<br>
    <input type="checkbox" name="list" value="2">Javascript<br>
    <input type="checkbox" name="list" value="3">Active Server Pages<br>
    <input type="checkbox" name="list" value="4">HTML<br>
    <input type="checkbox" name="list" value="5">SQL<br>
    
    <input type="button" value="Check All" onClick="CheckAll(true)">
    <input type="button" value="Uncheck All" onClick="CheckAll(false)">
    </body>
    </html>
    Or to be more specific for the checkbox range...
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Checkbox Settings </title>
    <script type="text/javascript">
    //<![CDATA[
    function CheckAll(flag) {
      var sel = document.getElementsByTagName('input');
      for (var i=0; i<sel.length; i++) {
        if (sel[i].name == 'list') { sel[i].checked = flag; }
      }
    }
    //]]>
    </script>
    
    </head>
    <body>
    <input type="checkbox" name="list" value="1">Java<br>
    <input type="checkbox" name="list" value="2">Javascript<br>
    <input type="checkbox" name="list" value="3">Active Server Pages<br>
    <input type="checkbox" name="list" value="4">HTML<br>
    <input type="checkbox" name="list" value="5">SQL<br>
    
    <input type="button" value="Check All" onClick="CheckAll(true)">
    <input type="button" value="Uncheck All" onClick="CheckAll(false)">
    </body>
    </html>
    Last edited by JMRKER; 03-05-2012 at 09:18 AM.

  3. #3
    Join Date
    Mar 2011
    Posts
    1,138
    You need to assign a unique name attribute to each of your checkboxes. Otherwise, the browser will only send one value in the form data. Good luck!
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Quote Originally Posted by rtrethewey View Post
    You need to assign a unique name attribute to each of your checkboxes. Otherwise, the browser will only send one value in the form data. Good luck!
    If that is the case, then change post #2 to the following:
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Checkbox Settings </title>
    <script type="text/javascript">
    //<![CDATA[
    function CheckAll(IDS,flag) {
      var obj = document.getElementById(IDS);
      var sel = obj.getElementsByTagName('input');
      for (var i=0; i<sel.length; i++) {
        if (sel[i].type == 'checkbox') { sel[i].checked = flag; }
      }
    }
    //]]>
    </script>
    
    </head>
    <body>
    <div id="CBoxList">
    <input type="checkbox" name="list1" value="1">Java<br>
    <input type="checkbox" name="list2" value="2">Javascript<br>
    <input type="checkbox" name="list3" value="3">Active Server Pages<br>
    <input type="checkbox" name="list4" value="4">HTML<br>
    <input type="checkbox" name="list5" value="5">SQL<br>
    <input type="button" value="Check All" onClick="CheckAll('CBoxList',true)">
    <input type="button" value="Uncheck All" onClick="CheckAll('CBoxList',false)">
    </div>
    </body>
    </html>

  5. #5
    Join Date
    Mar 2012
    Posts
    5
    the problem is not check all the check boxes. the script should reload the web page then check all the checkboxes in that web page.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Quote Originally Posted by gana View Post
    the problem is not check all the check boxes. the script should reload the web page then check all the checkboxes in that web page.
    So call the function onload.

  7. #7
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by JMRKER View Post
    So call the function onload.
    if i use onload function, all checboxes will be checked on loading of webpage. but, the condition is "all the checkboxes should checked on reloading of web page at specified time and unchecked after 30 seconds on reloads."

  8. #8
    Join Date
    Mar 2012
    Posts
    5

    read entered time value

    currently i'm working on tme function. where user input the time and the jsp file should read it and refresh the page at user entered time. now, i'm getting NaN. help. thanks in advance. here's my code

    <html>
    <head>

    <script type="text/javascript">//<![CDATA[
    function checkSessions(theElementCB, name) {
    if (hasCheckedCB(theElementCB, name)) {
    return true;
    } else {
    alert('<format:message key="js.err.select_one"/>');
    return false;
    }
    }
    function invalidateSessions(theElement) {
    if (checkSessions(theElement, 'sessionIds') && window.confirm('<format:message key="js.invalidate.confirm"/>')) {
    document.getElementById('sessionsFormAction').value='invalidateSessions';
    return true;
    } else {
    return false;
    }
    }
    function getCurrentTime()
    {
    var myDate = new Date();
    var mySecs = myDate.getSeconds();
    var curHour = myDate.getHours();
    var curMin = myDate.getMinutes();
    var suffix = "AM";

    if(mySecs < 10)
    mySecs = "0" + mySecs;

    if(curMin < 10)
    curMin = "0" + curMin;

    if(curHour == 12 && curMin >= 1)
    {
    suffix = "PM";
    }
    if(curHour == 24 && curMin >= 1)
    {
    curHour-= 12;
    suffix = "AM";
    }
    if(curHour > 12)
    {
    curHour-= 12;
    suffix = "PM";
    }

    var time = curHour + ":" + curMin + ":" + mySecs + " " + suffix;
    document.getElementById('time').innerHTML=(time);
    }
    //]]>
    </script>

    </head>
    <body>

    <body onload="setInterval('getCurrentTime()', 1000);">
    <div id="time"></div>

    <form action="<%= submitUrl %>" method="post" name="sessionsForm" id="sessionsForm">
    <div class="noprint" style="text-align: center;">
    <p>Reload Time : <input type="number" name="reloadTime" />
    <br/>
    <input type="submit" name="submit" value="Submit"/>
    <input type="hidden" name="saveReloadTime" value="<%=request.getParameter("reloadTime") %>"/></p>
    </div>
    </form>

    <% String reloadTime=request.getParameter("reloadTime"); %>
    <script type="text/javascript">
    function CheckAll(sessionIds,flag) {
    var obj = document.getElementById(sessionIds);
    var sel = obj.getElementsByTagName('input');
    for (var i=0; i<sel.length; i++) {
    if (sel[i].type == 'checkbox') { sel[i].checked = flag; }
    }
    }

    //enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
    var limit="00:10";
    //alert();

    if (document.images){
    var parselimit=limit.split(":")
    parselimit=parselimit[0]*60+parselimit[1]*1
    }

    function beginrefresh(){
    if (!document.images)
    return
    if (parselimit==1)
    window.location.reload()
    else{
    parselimit-=1
    curmin=Math.floor(parselimit/60)
    cursec=parselimit%60
    if (curmin!=0)
    curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
    else
    curtime=cursec+" seconds left until page refresh!"
    window.status=curtime
    setTimeout("beginrefresh()",1000)
    }
    }

    window.onload=beginrefresh
    {
    CheckAll('CBoxList',true);
    var t = setTimeout('resetCBox()',5000);
    }

    function resetCBox() {
    CheckAll('CBoxList',false);
    }

    </script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles