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

  • Home
  • SEARCH
  • 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 622551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:59:48+00:00 2026-05-13T18:59:48+00:00

I am having trouble checking checkboxes via JQuery – I have done this: if(element[1]==3)

  • 0

I am having trouble checking checkboxes via JQuery – I have done this:

if(element[1]==3)
  alert('#'+element[0]+'_1');//shows #chk_1
  $('#'+element[0]+'_1').attr('checked', true);
  $('#'+element[0]+'_2').attr('checked', true);
  $('#'+element[0]+'_3').attr('checked', true);    
}

I know it gets to that point because I can see the alert window with the correct variable name. I have also included JQuery and my firebug isn’t complaining about any errors, its just nothing is happening.

I have a few other functions which are active on a user click:

onclick="checker($(this).attr('id'));" 

Which essentially checks if a user is allowed to tick the checkbox but this shouldn’t get activated if I use the above method, correct? Plus I have done the automated ticks in such a way that those rules are preserved and the user is allowed to tick the checkboxes.

I am able to click the checkboxes and those rule checks are done correctly in the checker function.

HELP! I have no idea what’s wrong to be frank.

I appreciate nay help.

Update

With Pekka’s advice I have found that no JQuery function seems to be working for example:

$('#'+element[0]+'_1').hide();

Did nothing. So it means the ID of the element has a problem? But the element does exist and its the correct name!

I have given the checkbox a value and tried this:

alert($('#'+element[0]+'_1').val());

I get “undefined” returned! Why?

Update 2

Testing if it has been checked:

$('#'+element[0]+'_1').attr('checked', true);

if ($('#'+element[0]+'_1'+':checked').val() !== null) {
   alert('#'+element[0]+'_1');
}

This showed me the alert box. So it seems it is checked but I can not see it?

Update – Solution

Don’t be a noob like me and wrap JQuery code with: $(document).ready(function() {

I managed to waste everyones time having not done this, I think that bit of code is going to become standard for me from now on.

Thank you everyone for the continued help! 🙂 I’ll have to give to the person [lillq] that mentioned document load!

  • 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-13T18:59:49+00:00Added an answer on May 13, 2026 at 6:59 pm

    One thought is:

    If you have more than one chk_1, chk_2, chk_3 ids in your html, the $('#'+element[0]+'_2').attr('checked', true); will only effect the first one in your document.

    I ran the code you supplied as such:

    html

    ...
    <body>
        <input type="checkbox" id="el1" onclick="checker($(this).attr('id'));" />Text<br />
        <input type="checkbox" id="el2" onclick="checker($(this).attr('id'));" />Text<br />
        <input type="checkbox" id="el3" onclick="checker($(this).attr('id'));" />Text<br />
    </body>
    </html>
    <script type="text/javascript">
        init();
    </script>
    

    javascript

    function init () {
        $('#el1').attr('checked', true); 
        $('#el2').attr('checked', true); 
        $('#el3').attr('checked', true); 
    }
    function checker(id)
    {
        alert(id);
    }
    

    The init function here checks all of the boxes after the document has loaded. Is there any other info that you could give us help out.

    I like to reduce examples like this to the simplest code that fails.

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

Sidebar

Related Questions

I'm having trouble checking hidden checkboxes in IE. This is the base html: <input
I'm having trouble checking out a project via svn. I have a repository set
With WebDriver from Selenium 2.0a2 I am having trouble checking if an element is
Thanks for checking out my problem... I'm having trouble submitting a login form via
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I'm having trouble with global variables in php. I have a $screen var set
I'm having trouble checking in PHP if a value is is any of the
As title says, im having trouble with my junit tests passing for checking if
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the

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.