开发者

change textarea value doesn't work

开发者 https://www.devze.com 2023-01-20 19:56 出处:网络
So, what i\'m trying to do is adding a Javascript snippet in wordpress admin new post page, so that when i click a button it fills up the CONTENT textarea where the body of the post goes. Adding it is

So, what i'm trying to do is adding a Javascript snippet in wordpress admin new post page, so that when i click a button it fills up the CONTENT textarea where the body of the post goes. Adding it is not a problem, i've already done this, but for some reason filling up the content field doesn't work.

The textarea from the wordpress admin page is:

<textarea rows='10' class='theEditor' cols='40' name='content' tabindex='2' id='content'></textarea>

Also i tried to fill up the POST_TITLE input and it works fine. I know the problem might come from the JS scripts of wordpress for the content field, because it's not a simple textarea, it's a JS text editor. Any suggestions? And the JS snippet is:

<script type="text/javascript">

    $("form").submit(function(){
        $("input[name$='post_title']").val("a letter");
        document.getElementById('content').innerHTML = "testing";
   开发者_开发问答     return false;
    });

</script>

PS: jquery library already included


Use .val() to set (or get) the value of an input type element, including <textarea> and <select>, like this:

$("form").submit(function(){
    $("input[name$='post_title']").val("a letter");
    $('#content').val("testing");
    return false;
});


You shouldn't mix jQuery and standard dom calls? Try this:

$("#content").text("hi");

Worked fine for me from the Firebug console.

Update: Sorry guys, missed the part that it's TinyMCE we're talking about. Here's the code that should be used to update both the hidden textarea AND the visual editor itself:

jQuery("#content").text("Your text goes here");
tinyMCE.activeEditor.execCommand('mceSetContent', false, jQuery("#content").text());

For more info check out the TinyMCE Commands API

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号