0

I know of a way to add a character counter to the excerpt metabox in WP, but I would like to add a character counter to a custom metabox. The following script is getting the counter in the metabox, which has tabs, but it isn't counting. Any help would be great. Thanks, it's my first question in StackExchange.

jQuery(document).ready(function(){
    jQuery("#bgnp-metabox-tabs").after("<p style=\"text-align:left;\"><small>Excerpt length: </small><input type=\"text\" value=\"0\" maxlength=\"3\" size=\"3\" id=\"bgnp-metabox-tabs\" readonly=\"\"> <small>character(s).</small></p>");
    jQuery("#feed").val(jQuery("#excerpt").val().length);
    jQuery("#bgnp-metabox-tabs").keyup( function() {
        jQuery("#bgnp_metabox_tabs").val(jQuery("#excerpt").val().length);
    });
});

The counter appears in all of the tabs, but I only want it in the tab "#feed"

1 Answer 1

0

I assume the #feed element is the one in which you want the count value to appear? If so, change the #bgnp_metabox_tabs entry in your keyup function to #feed.

EDIT:

For some reason, I wasn't seeing all the code when I first read the problem.

The id of your placeholder for the character count is the same as the id on which you are calling the after function. I would change the placeholder id to something like char-count.

You can then target that placeholder with jQuery(#feed #char-count) to get it to be shown only on the feed tab (assuming the feed tab has an id of #feed)

3
  • I am very new to JQuery, so I just want to make sure I am replacing #feed #char-count correctly. It would replace #bgnp-metabox-tabs in the jQuery("#bgnp-metabox-tabs").keyup( function() { line?
    – user54188
    Commented Jun 15, 2014 at 15:22
  • Just to clarify: #feed is the name of the correct tab. #excerpt is the place where the text to be counted is located. #char-count is the place where you want the character count number to be placed. However, I don't know what is happening with the keyup part. Is the user typing more text somewhere? If these things are true: Change the id where you have the "Excerpt Length" to char-count. The following line should be adjusted as follows: jQuery("#thePlaceUsersType").keyup( function() { jQuery("#feed #char-count").val(jQuery("#excerpt").val().length);
    – leromt
    Commented Jun 16, 2014 at 1:19
  • I would love to provide some feedback, but the caching is really giving me a problem on js and css. Cloudflare is in development mode, but it doesn't seem to be making any changes.
    – user54188
    Commented Jun 16, 2014 at 23:08

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.