0

I'm making this link, that sends an extra parameter to the admin-panel, as such:

/wp-admin/post-new.php?post_type=CPT&msscategory=40

Then I enqueue some javascript on that given page as such (added as an action to admin_footer):

$screen = get_current_screen();

if ( in_array( $screen->id, array( 'CPT' ) ) && $screen->action == 'add' ) {
  wp_enqueue_script( 'mark-default-category', get_stylesheet_directory_uri() . '/assets/js/custom.js', array( 'jquery' ) );
}

And then I would like to check the category-checkbox that has id=39. But I can't get my head around the HTML. It looks like this:

<li id='category-39'>
  <label class="selectit">
    <input value="39" type="checkbox" name="post_category[]" 
           id="in-category-39" /> 
    Page 1 (depth 0)
  </label>
  <ul class='children'>
    <li id='category-40' class="popular-category">
      <label class="selectit">
        <input value="40" type="checkbox" name="post_category[]" 
               id="in-category-40" /> 
        Page 2 (depth 1)
      </label>  
      <ul class='children'>
        <li id='category-41'>
          <label class="selectit">
            <input value="41" type="checkbox" name="post_category[]"
                   id="in-category-41" /> 
            Page 3 (depth 2)
          </label>
        </li>
        <li id='category-42'>
          <label class="selectit">
            <input value="42" type="checkbox" name="post_category[]"
                   id="in-category-42" /> 
            Page 3 (depth 2)
          </label>
        </li>
        <li id='category-43'>
          <label class="selectit">
            <input value="43" type="checkbox" name="post_category[]" 
                   id="in-category-43" /> 
            Page 4 (depth 2)
          </label>
        </li>
      </ul>
    </li>
  </ul>

As can be seen, then the input-checkbox has no attribute (attr) or property (prop) called checked. This is a label before it's clicked:

 <label class="selectit">
   <input value="43" type="checkbox" name="post_category[]" 
          id="in-category-43" /> 
   Page 4 (depth 2)
 </label>

And this is one after it has been clicked:

 <label class="selectit">
   <input value="43" type="checkbox" name="post_category[]" 
          id="in-category-43">
     ::before
   </input> 
   Page 4 (depth 2)
 </label>

So all that happens is that the input-field no longer self-closes, - but that ::before is added.

Now, - how do I do that with jQuery?

This is the closest I've gotten.

var finding_the_checkbox = jQuery('#category' + just_the_category_ID ).find("label");
findind_the_checkbox.click()

... While formulating this, then I found the solution. I'll post that below (since I couldn't find an answer anywhere else. The internet needs this!

1 Answer 1

0

This solved it:

jQuery(document).ready(function() {
  jQuery( '#in-category-43' ).prop('checked', true);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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