最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Access post title from custom meta box on title change

programmeradmin8浏览0评论

I have a custom meta box as part of my post editor. When the user changes the post title, I want to reflect this change in the content inside the meta box. I have seen plugins like Yoast do this but im not sure how.

I have tried using a jQuery event to do this but even if i wait for document.ready, it doesnt bind itself to the element.

jQuery(".editor-post-title__input").keyup(function(){
    console.log(jQuery(".editor-post-title__input").val());
});

I have a custom meta box as part of my post editor. When the user changes the post title, I want to reflect this change in the content inside the meta box. I have seen plugins like Yoast do this but im not sure how.

I have tried using a jQuery event to do this but even if i wait for document.ready, it doesnt bind itself to the element.

jQuery(".editor-post-title__input").keyup(function(){
    console.log(jQuery(".editor-post-title__input").val());
});
Share Improve this question edited Apr 16, 2019 at 17:59 Alexander Holsgrove 1,9091 gold badge15 silver badges25 bronze badges asked Apr 16, 2019 at 10:22 user1889580user1889580 4621 gold badge5 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The event listener you have added will only attach to the available .editor-post-title__input elements, and won't attach to any dynamically added elements. You are better to move the listener to the body as follows:

jQuery('body').on('keyup', '.editor-post-title__input', function(event){ 
   console.log(jQuery(this).val());
});

Given the admin area is moving to use more React components, it's a good habit to get into.

发布评论

评论列表(0)

  1. 暂无评论