Jump to content

check if any checkbox has been checked, but id for each checkbox is random


Recommended Posts

Hi All,

 

I have a page that has 20 checkboxes, sometimes with multiple pages (pagination).

The "id" for each checkbox is checkbox_XXXX where XXXX is the mysql id.

 

It's similar to a shopping cart, where the user can select one or more checkboxes, and click "Add to Shopping Cart".

 

I'd like to have a JS that verifies at least one checkbox has been selected before running the $_POST code to add to shopping cart, and if not, just throw an alert with a message.

 

Thanks

var input = document.getElementById('container').getElementsByTagName('input');

var checkboxes = [];

for(x in input){

if(input[x].type == 'checkbox') checkboxes.push(input[x]);

}

//now you have an array of all of the checkboxes inside of container. loop through that array to do some stuff

just a suggestion why not render the id's ascending its the value that should matter for you input not the id

 

another way is just to loop through all input elements in your form

sorry about that

 

var input = document.getElementById('container').getElementsByTagName('input');
var checkboxes = [];
for(x in input){
if(input[x].type == 'checkbox') checkboxes.push(input[x]);
}
//now you have an array of all of the checkboxes inside of container. loop through that array to do some stuff

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.