views:

45

answers:

2

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

+2  A: 

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;
});
Nick Craver
I've already tried that and it didn't work.
flowerpower
@flowerpower - Are you getting a JavaScript error? Try wrapping all of the above code inside a `(function($) {` ...above code... `})(jQuery);` block, WordPress usually uses `.noConflict()` on the jQuery include.
Nick Craver
No i don't get any error, i wrapped all the code inside what you gave me but had no luck at all.
flowerpower
@flowerpower - Is the `<form>` added to the page dynamically, via ajax for example?
Nick Craver
No, it's just under the <script> tag, as i said $("input[name$='post_title']").val("a letter") works fine, the title input changes its value to "a letter" when i submit the form, the content textarea is the only one that's not working
flowerpower
@flowerpower - Is it possible there is more than one `id="content"` element? That *really* sounds like your symptoms.
Nick Craver
No, i checked that and it's only one, here's the whole div where the text editor is wrapped: http://pastebin.com/SrAmqYAu .. it's copy paste as it is on wordpress admin post page
flowerpower
@flowerpower - I don't see a `<form>` in there...how is this related to your current code?
Nick Craver
There is actually a form on the same page, look here http://pastebin.com/Q17U6gZS
flowerpower
A: 

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

kovshenin
you tried it on wordpress?
flowerpower
Sorry about that, missed the part where you said you work with the visual editor. Updated the post.
kovshenin