I have a site with CPT (short for custom post type) “bagp_deals” and custom taxonomies
“ba_locations” and “ba_cats”
basically Its post type of “Deals” with “Location” and “Categories” as hierarchical taxonomies.
On the default edit screen i want to limit the selection to just one of each (one location and one category) and i’m trying to do that with JQuery,
i notice that the field custom taxonomy of ba_locations is named “tax_input[ba_locations][]” and so far i have this code:

jQuery("input[name=tax_input[ba_locations][]]").click(function () {
    selected = jQuery("input[name=tax_input[ba_locations][]]").filter(":checked").length;
    if (selected > 1){
        jQuery("input[name=tax_input[ba_locations][]]").each(function () {
                jQuery(this).attr("checked", false);
        });
        jQuery(this).attr("checked", true);
    }
});

witch is suppose to limit the checkbox selection to one.
For some reason i can’t get this to work.

The Question

So the question is why isn’t this working ?
or do you have a better solution to limit the selection to just one?

any help is appreciated.

update:

this is the working code i used:

jQuery("input[name=\"tax_input[ba_locations][]\"]").click(function () {
    selected = jQuery("input[name=\"tax_input[ba_locations][]\"]").filter(":checked").length;
    if (selected > 1){
        jQuery("input[name=\"tax_input[ba_locations][]\"]").each(function () {
                jQuery(this).attr("checked", false);
        });
        jQuery(this).attr("checked", true);
    }
});

5 s
5

Instead of hacking it with jQuery, a more reliable solution would be to replace the meta box with your own, in PHP.

Anyway, the problem is most likely with the ‘[‘ and ‘]’ characters in the selector:

"input[name=tax_input[ba_locations][]]"

could be rewritten as

"input[name=tax_input\\[ba_locations\\]\\[\\]]"

See https://stackoverflow.com/questions/2786538/need-to-escape-a-special-character-in-a-jquery-selector-string

Leave a Reply

Your email address will not be published. Required fields are marked *