admin管理员组

文章数量:1432566

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.

本文标签: pluginsAccess post title from custom meta box on title change