By FosAvance


2019-06-12 06:44:53 8 Comments

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?

1 comments

@Sally CJ 2019-06-12 07:57:15

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:

@FosAvance 2019-06-12 08:20:44

Thank you very much, I'll test it out :)

@Sally CJ 2019-06-12 08:32:37

Sure, take your time. :)

@FosAvance 2019-06-12 15:58:08

It worked. yeeeey :D BTW where did you find that info, is it anywhere on WP official page?

@Sally CJ 2019-06-12 22:52:02

I decided to update the answer with the resources which helped me. I hope that helps you. ;)

Related Questions

Sponsored Content

6 Answered Questions

[SOLVED] How Do I Set the Page Title Dynamically?

  • 2011-11-07 18:40:15
  • Alex Cook
  • 49956 View
  • 14 Score
  • 6 Answer
  • Tags:   seo title

1 Answered Questions

3 Answered Questions

[SOLVED] Change title tag dynamically or within plugin?

  • 2015-07-12 19:47:23
  • Goodbytes
  • 7012 View
  • 0 Score
  • 3 Answer
  • Tags:   title

1 Answered Questions

[SOLVED] Dynamically change page title from URL param

1 Answered Questions

[SOLVED] Remove post title input from edit page

  • 2013-08-17 13:58:51
  • wamps
  • 2668 View
  • 4 Score
  • 1 Answer
  • Tags:   posts title

1 Answered Questions

[SOLVED] Images inside post title

  • 2012-07-30 14:44:37
  • slm_92
  • 113 View
  • 0 Score
  • 1 Answer
  • Tags:   posts title

Sponsored Content