jQuery store list of checkbox values in div

Share this article

Just say you wanted to store a list in a text area so that when a checkbox is ticked it appended the item to the list. Likewise when the checkbox was unchecked in removed the item from the list. This is how you might go about doing such a crazy thing! ;-)

Demo

/*when a user selects interest in an addtional service, add this to the additionalServices div*/
$('input[type="checkbox"]').bind('change', function() {
	var alsoInterested = '';
	$('input[type="checkbox"]').each(function(index, value) {
		if (this.checked) {
			/*add*/ /*get label text associated with checkbox*/
			alsoInterested += ($('label[for="'+this.name+'"]').html() + ', ');
		}
	});
	if (alsoInterested.length > 0) {
		alsoInterested = 'I am also interested in booking: ' + alsoInterested.substring(0,alsoInterested.length-2) + '.';
	} else {
		alsoInterested = 'I am not interested in additional services.';
	}
	
	$('#additionalServices').html(alsoInterested);
	//console.log($('#additionalServices').html());
});
Code is based on the following HTML:

	
	Bannanas

	
	Car Monkeys
	
	
	Apes
	
	
	Car Repair

	

Frequently Asked Questions about jQuery and Checkbox Values

How can I get the value of a checkbox using jQuery?

To get the value of a checkbox using jQuery, you can use the .val() method. This method returns the value attribute of the selected element. Here’s an example:

$('input[name="checkboxName"]:checked').val();

In this example, checkboxName is the name of your checkbox. This will return the value of the checkbox that is currently checked. If no checkbox is checked, it will return undefined.

How can I select all checkboxes using jQuery?

To select all checkboxes using jQuery, you can use the :checkbox selector. This selector selects all checkbox inputs. Here’s an example:

$('input:checkbox').prop('checked', true);

This will check all checkboxes on the page.

How can I store the values of multiple checked checkboxes in a div?

To store the values of multiple checked checkboxes in a div, you can use the .each() method in combination with the :checked selector. Here’s an example:

var values = [];
$('input[name="checkboxName"]:checked').each(function() {
values.push($(this).val());
});
$('#divId').text(values.join(', '));

In this example, checkboxName is the name of your checkboxes and divId is the id of your div. This will store the values of all checked checkboxes in the div.

How can I add the values of checked checkboxes to a database using jQuery?

jQuery itself cannot interact directly with a database, as it is a client-side language. However, you can use jQuery to send an AJAX request to a server-side script (like PHP), which can then interact with the database. Here’s an example:

var values = [];
$('input[name="checkboxName"]:checked').each(function() {
values.push($(this).val());
});
$.ajax({
url: 'server-side-script.php',
type: 'post',
data: {checkboxValues: values}
});

In this example, checkboxName is the name of your checkboxes and server-side-script.php is your server-side script.

How can I check or uncheck a checkbox using jQuery?

To check or uncheck a checkbox using jQuery, you can use the .prop() method. This method sets properties on the selected elements. Here’s an example:

$('input[name="checkboxName"]').prop('checked', true); // To check
$('input[name="checkboxName"]').prop('checked', false); // To uncheck

In this example, checkboxName is the name of your checkbox. This will check or uncheck the checkbox.

How can I check if a checkbox is checked using jQuery?

To check if a checkbox is checked using jQuery, you can use the :checked selector. This selector selects all checked checkboxes. Here’s an example:

if ($('input[name="checkboxName"]').is(':checked')) {
// Checkbox is checked
} else {
// Checkbox is not checked
}

In this example, checkboxName is the name of your checkbox. This will check if the checkbox is checked.

How can I get the values of all checked checkboxes using jQuery?

To get the values of all checked checkboxes using jQuery, you can use the .each() method in combination with the :checked selector. Here’s an example:

var values = [];
$('input[name="checkboxName"]:checked').each(function() {
values.push($(this).val());
});

In this example, checkboxName is the name of your checkboxes. This will store the values of all checked checkboxes in the values array.

How can I toggle a checkbox using jQuery?

To toggle a checkbox using jQuery, you can use the .click() method. This method simulates a mouse click on an element. Here’s an example:

$('input[name="checkboxName"]').click();

In this example, checkboxName is the name of your checkbox. This will toggle the checkbox.

How can I select a specific checkbox using jQuery?

To select a specific checkbox using jQuery, you can use the :eq() selector. This selector selects an element with a specific index. Here’s an example:

$('input[name="checkboxName"]:eq(0)').prop('checked', true);

In this example, checkboxName is the name of your checkboxes and 0 is the index of the checkbox. This will check the first checkbox.

How can I handle a checkbox click event using jQuery?

To handle a checkbox click event using jQuery, you can use the .click() method. This method attaches an event handler function to the click event. Here’s an example:

$('input[name="checkboxName"]').click(function() {
if ($(this).is(':checked')) {
// Checkbox is checked
} else {
// Checkbox is not checked
}
});

In this example, checkboxName is the name of your checkbox. This will handle the checkbox click event.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week