Jump to content

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


solarisuser

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.