Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6320975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:04:09+00:00 2026-05-24T16:04:09+00:00

I have created the following function to check the first value of the check

  • 0

I have created the following function to check the first value of the check box when loading a page –

function defaultCheck(){
      document.checkBoxForm.list[0].checked = true;
      var val=document.checkBoxForm.list[0].value;
      showtotal[0] = document.checkBoxForm.list[0].value;
      show(val);
      editVal();
      //alert('ajhsdjha');
 }

I have used the function in html page load by <body onload="defaultCheck()">.

My problem is – when there are more than one values in the list the function by default selects the first one but when there is just one value in the list, it does not select it. The values of check boxes are dynamically retrieved from database by –

 <?php
     while($row=mysql_fetch_array($levelq)) {?>
        <input type='checkbox' name="list" value="<?=$row['tag']?>"  
               id="<?=$row['tag']?>" onclick="" />
        <?  echo $row['tag'].' '.'<br/><br/>'; 
     }
?>

I have no idea why is it behaving so weird. Can anyone help me out please?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:04:10+00:00Added an answer on May 24, 2026 at 4:04 pm

    Like @cwallenpoole said, you should be doing this in PHP.

    If you still want to do it in JS, here you go:

    function defaultCheck(){
          if(document.checkBoxForm.list.length > 1) {
              document.checkBoxForm.list[0].checked = true;
              var val=document.checkBoxForm.list[0].value;
              showtotal[0] = document.checkBoxForm.list[0].value;
          } else {
              document.checkBoxForm.list.checked=true;
              var val=document.checkBoxForm.list.value;
               showtotal[0] = document.checkBoxForm.list.value;
          }
          show(val);
          editVal();
          //alert('ajhsdjha');
     }
    

    This happens because, when there is only 1 checkbox, list refers to that checkbox, NOT the array of checkboxes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==
I have created a plugin that creates a check box image based based on
I have created a web page Default.aspx in which I have taken fields: First
I have created a printf -like function which takes the following arguments: One mandatory
I have the following function: CREATE FUNCTION [dbo].[ListStockBySubCategory] ( @CategoryID varchar(10), @SubCategoryID varchar(10), @startRowIndex
I have the following function which creates a std::vector of iterators into another container:
I have the following function that takes a number like 5 and creates a
I have a strange one. Create a new form. Then add the following function
I have the following scalar function in MS SQL 2005: CREATE FUNCTION [dbo].[Distance] (
I have the following templated function... template< class T > T *create_object( lua_State *L

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.