1

I have a textarea in admin post page when creating new post, that's title:

<textarea id="post-title-0" class="editor-post-title__input" placeholder="Add title" rows="1" style="overflow: hidden; overflow-wrap: break-word; resize: none; height: 94px;">vbhgfhfgh</textarea>

I want to update title on the fly using jQuery. I tried

$('#post-title-0').val('some value');
$('#post-title-0').text('some value');

I also tried using plain javascript.

val() updates title but when I click on title it is being erased. Is there anyway to update title via JS?

2

The block editor is restoring back the title that was last saved (or typed manually into the textarea), so with the block editor, you can change the title dynamically using this code:

wp.data.dispatch( 'core/editor' ).editPost( { title: 'Title here' } )

PS: You should make sure your JS file has the wp-editor, wp-edit-post or wp-data as part of the dependencies.

UPDATE

Here are the resources which helped me identify the above solution/code:

  • Thank you very much, I'll test it out :) – FosAvance Jun 12 at 8:20
  • Sure, take your time. :) – Sally CJ Jun 12 at 8:32
  • 1
    It worked. yeeeey :D BTW where did you find that info, is it anywhere on WP official page? – FosAvance Jun 12 at 15:58
  • I decided to update the answer with the resources which helped me. I hope that helps you. ;) – Sally CJ Jun 12 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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