Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
    }
});
share|improve this question
Maybe you're looking to do something similar to what was done here: wordpress.org/support/topic/… – t31os Jan 17 '11 at 2:08
I already figured it out, but thanks. – Bainternet Jul 4 '11 at 7:44

3 Answers

up vote 4 down vote accepted

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 http://stackoverflow.com/questions/2786538/need-to-escape-a-special-character-in-a-jquery-selector-string

share|improve this answer
Thanks but nope that's not it' i have tried that already. but i guess i have no other option then replacing the meta box with my own. – Bainternet Jan 17 '11 at 6:17
i guess that was the issue all along, thanks. – Bainternet Jul 4 '11 at 7:44

Not sure if you ever found a solution to this, but I needed to do the same thing. I took your jQuery and added quotations for the name + escaped them. Seems to work fine for me, so thanks for the original jQuery :)

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

My taxonomy input was called location and not ba_locations.

share|improve this answer

This is what I threw in my functions, but I don't seem to be getting the results expected.

<?php add_action( 'admin_head', 'cat_sel' ); function cat_sel() {    ?>

<script type="text/javascript">
jQuery(document).ready(function($) {
  jQuery("input[name=\"tax_input[rooms][]\"]").click(function () {
    selected = jQuery("input[name=\"tax_input[rooms][]\"]").filter(":checked").length;
    if (selected > 1){
        jQuery("input[name=\"tax_input[rooms][]\"]").each(function () {
                jQuery(this).attr("checked", false);
        });
        jQuery(this).attr("checked", true);
    }
  });
});

</script>
<?php } ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.