OK, I think I get what you need. First of all, if you're using jQuery, you can use any CSS selector as your selector to get your element. For instance, to get an input with the id
of checkbox123
, you would simply:
$('#checkbox123')
However, if you wanted a collection of input checkbox
es within a ul
, then you could select like:
$('ul input[type=checkbox]')
Finally, determine whether or not it's checked is extremely simple.
var checked = $('#checkbox123').is(':checked');
// see your result in developer console with
console.log(checked); // will be `true` or `false`
If you're trying to get the span (with text Select This
) after, on a button click, you might try something like:
$('button').click(function(e) {
$('ul input[type=checkbox]:checked').each(function(i) {
var span = $(this).next('span');
alert(span.text())
});
})
Last call, if you need to change the state of a checkbox, use .prop
on checked
with a boolean value. Such as:
$('#checkbox123').prop('checked', true); // will make it checked
$('#checkbox123').prop('checked', false); // will uncheck it
I believe this may have been updated since I last answered question, so I'll update answer here to show what's obviously missing.
Finally, using jQuery to fire an event, such as click
is extremely simple. There are also a couple ways to do such, some basically the same thing.
The easiest is to use .click()
. It's as simple as:
$('#checkbox123').click();
Next use is .trigger()
. Basically same thing as .click
. Really this is just about personal use in readability.
$('#checkbox123').trigger('click');
Keep in mind, if you're using this, and it's not working. Something maybe wrong with your selector. One way to know for sure is to console log it. If you don't see a jQuery OBject Element printed to console containing the element you expect, then you know your selector isn't working. Keep in mind, this will show a jQuery Object regardless, but it will not have an HTML element in it.
console.log($('#checkbox123').trigger('click'));
If exist, will look something like:
[input#checkbox123, context: document, selector: "#content", jquery: "1.9.......and so on for rest of object
If not exist, will look something like:
[context: document, selector: "#checkbox123", jquery: "1.9.......and so on for rest of object