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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:25:09+00:00 2026-05-26T00:25:09+00:00

I am trying to get the value from a checkbox using javascript. I want

  • 0

I am trying to get the value from a checkbox using javascript.
I want only one checkbox value to be passed to the javascript function, and if multiple are selected, an alert box informing that only one box can be checked for the function.
I’ve tried this:

var publish_trigger = document.querySelector("#publish_trigger");
publish_trigger.onclick = function() {
  var _posts = document.getElementsByName('post_id[]');
  var check = _posts.checked;
  var boxes = _posts.length;
  var txt = "";
  if(check.length > 1) {
    alert("Only one at a time");
  } else {
    for (i = 0; i < boxes; i++) {
      if (_posts[i].checked) {
        txt = txt + _posts[i].value + " "
      }
    }
  }
  alert(txt);
  return false;
}
  • 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-26T00:25:10+00:00Added an answer on May 26, 2026 at 12:25 am

    This code is wrong:

    var _posts = document.getElementsByName('post_id[]');
    var check = _posts.checked;  
    

    getElementsByName() returns a NodeList (effectively an array) of elements, so your variable _posts doesn’t have a checked property. You need to loop through _posts to count the checked property on the individual elements within _posts.

    You already have a for loop so add the validation in there:

    var count = 0;
    for (var i = 0; i < boxes; i++) {
       if (_posts[i].checked) {
          if (++count > 1) {
             alert("Only one checkbox may be checked at a time.");
             return false;
          }
          // I don't know what you're trying to do with the following line
          // but I've left it in.
          txt = txt + _posts[i].value + " " 
       }
    }
    

    (Note: unrelated to your question, you should declare the loop counter i within your function otherwise it will be global and might lead to hard to debug problems if you are using it in other places too.)

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

Sidebar

Related Questions

i am trying to get checkbox value ,using c# In grid view the datasource
im trying to get the integer value from the following xml structure (its only
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying to insert a time only value, but get the following error
I am trying to get value from the databse and assign it to the
I am trying to get value from an HTML list <div id=test> <ul> <li>C:\
I'm trying to get a value from a variable inside some files, here is
I'm trying to get all property names / values from an Outlook item. I
I am trying to get key-value-observing to work for an NSMutableArray. Below is the
i have a label in my form, and im trying to get the value

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.