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 6137307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:42:34+00:00 2026-05-23T17:42:34+00:00

I have the following code to check a checkbox as soon as the user

  • 0

I have the following code to check a checkbox as soon as the user types something in the textbox. This works fine for a single text box.

function activateCheckbox() {
        if (document.myForm.box1.checked == false) {
            document.myForm.box1.checked = true;
        }
    }
    <tr>
        <td><input type="text" id="mySearch1" name="mySearch1" size="40" onkeypress="activateCheckbox()"/></td>
        <td><input type="checkBox" id="box1" name="box1"/></td>
    </tr>

However suppose there are more than one text boxes with a checkbox against each one and I want that only the corresponding checkbox should be checked. I modified the above code as shown below by passing a parameter to the function but it’s not working, because “document.myForm.id.checked” doesn’t work as it accepts the checkbox name instead of “id”. Please suggest if there are better alternatives or how do I modify my code to make it working?

function activateCheckbox(id) {
        if (document.myForm.id.checked == false) {
            document.myForm.id.checked = true;
        }
    }
    <tr>
        <td><input type="text" id="mySearch1" name="mySearch1" size="40" onkeypress="activateCheckbox('box1')"/></td>
        <td><input type="checkBox" id="box1" name="box1"/></td>
    </tr>
    <tr>
        <td><input type="text" id="mySearch2" name="mySearch2" size="40" onkeypress="activateCheckbox('box2')"/></td>
        <td><input type="checkBox" id="box2" name="box2"/></td>
    </tr>
    <tr>
        <td><input type="text" id="mySearch3" name="mySearch3" size="40" onkeypress="activateCheckbox('box3')"/></td>
        <td><input type="checkBox" id="box3" name="box3"/></td>
    </tr>
  • 1 1 Answer
  • 0 Views
  • 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-23T17:42:34+00:00Added an answer on May 23, 2026 at 5:42 pm

    You have a couple of options. You can do:

    function activateCheckbox(id) {
        if (document.myForm[id].checked == false) {
            document.myForm[id].checked = true;
        }
    }
    

    …or personally, I think this is a bit more clear:

    function activateCheckbox(id) {
        var checkbox = document.getElementById(id);
        if (! checkbox.checked) {
            checkbox.checked = true;
        }
    }
    

    The first approach works because in JavaScript obj.someProperty means the same semantically as obj["someProperty"]. So if you have a variable that stores the name of the property you want to access, you can always do obj[name] to access the property.

    The second approach is just finding the checkbox in the DOM by its id. It seems cleaner to me, since you are setting the id of each checkbox anyways and since you called your variable “id”.

    Also note that your if statement is superfluous. The checked attribute will only ever be true or false (you can subvert this by storing other things there, but that’s a completely separate topic). So setting it to true whenever it is false is logically equivalent to always setting it to true, and you can implement your handler function with a single line, like:

    function activateCheckbox(id) {
        document.getElementById(id).checked = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code to check a checkbox when user presses any key. However
I have used the following code to generate some dynamic checkboxes. This works for
I have the following code that works perfectly. It allows the user to 'like'
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code: public static void ProcessStep(Action action) { //do something here if
I have a WebBrowser , a check box, a button and the following code:
I have the following code. When the checkbox is checked (which is hidden), when
I have the following HTML code: <div class=member> <input type=checkbox value=64 style=display:none; name=memberId[] />
I have the following code snippet which is working fine: ifstream NDSConfig( NDS.config )
I have the following HTML code: <input type=checkbox value=1 /> <input type=checkbox value=2 />

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.