Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference HTML Elements by name (with square brackets in them) via javascript

Hey everyone, I am trying to hide/show different html elements (div, etc...) based on whether a checkbox is checked or if a specific value is chosen from a dropdown box. I was wondering if someone can help me out. The html element is defined along the lines of this (below), and i'm not sure how to reference it by name with brackets in it. The page i'm using has jquery enabled, and i'd like to use it if possible. Thanks!

<input type="checkbox" name="addons[2]" />

Also - I cannot modify the checkbox's code.

like image 577
Anand Capur Avatar asked Feb 27 '23 19:02

Anand Capur


1 Answers

jQuery to check if element is checked:

$("input[name='addons[2]']").attr("checked")

jQuery to loop over such elements that are checked:

$("input[name^='addons']:checked").each(function() {
    // ...
});
like image 61
tanerkay Avatar answered Mar 02 '23 12:03

tanerkay