开发者

Get value of tinymce textarea with jquery selector

I want to get the value of a tinymce textarea

<textarea id="thetextarea"></textarea>

on key up in order to feed it into a show-preview script using:

function showPreview(value) {

    $("#preview-container").load("/material-preview.php", {s:value});

}
$('thetextarea').live("keyup",function (e) {

        var material = this.value;
        showPreview(material);

        return false;

    });

If I try to select the textarea id thetextarea it doesnt work (works if I dont make it an tinymce-field).

with firebug I see that the text, when the textarea is tinymce-converted, is in:

<body id="tinymce" class="mceContentBody"></body>

but this does not work either, (nor does $('#tinymce'))

 $('mceContentBody').live("keyup",function (e) {

            var material = this.value;
            showPreview(material);

            return false;

        });

HTML code (from firebug) after tinyMCE is applied as requested

 <textarea id="material-input" class="mceEditor text" style="width: 310px ! important; height: 250px ! important; display: none;" name="material" aria-hidden="true"></textarea>
      <span id="material-input_parent" class="mceEditor defaultSkin" role="application" aria-labelledby="material-input_voice">
      <span id="material-input_voice" class="mceVoiceLabel" style="display:none;">Rich Text Area</span>
      <table id="material-input_tbl" class="mceLayout" cellspacing="0" cellpadding="0" role="presentation" style="width: 3开发者_如何学运维10px; height: 250px;">
        <tbody>
          <tr class="mceFirst" role="presentation">
          <tr>
          <td class="mceIframeContainer mceFirst mceLast">
            <iframe id="material-input_ifr" frameborder="0" src="javascript:""" allowtransparency="true" title="Rich Text Area. Press ALT F10 for toolbar. Press ALT 0 for help." style="width: 100%; height: 206px;">
            <html>
             <head xmlns="http://www.w3.org/1999/xhtml">
               <body id="tinymce" class="mceContentBody " contenteditable="true" spellcheck="false" dir="ltr">
                  <!-- the text inside tinymce textarea -->
                </body>
            </iframe>
           </td>
           </tr>
           <tr class="mceLast">
         </tbody>
       </table>
    </span>


Call

tinyMCE.triggerSave();

after that you'll be able to use jquery selector

$("#yourtextareaID").val();


tinyMCE.get('yourTextAreaId').getContent();


See the following link on how to define the onKeyUp event to work with TinyMCE:

http://www.tinymce.com/wiki.php/API3:event.tinymce.Editor.onKeyUp

Essentially when you initialize tinyMCE you define your onKeyUp event handler. I think a regular selector won't work here since the text is inside a separate iFrame. The TinyMCE API lists a method .getContent() that may work. ie. Something like this:

tinyMCE.init({
   setup : function(ed) {
      ed.onKeyUp.add(function(ed, e) {
          //showPreview ( $('.mceContentBody').val() );
          showPreview (tinyMCE.activeEditor.getContent({format : 'raw'}) );

      });
   }
});

Also see: http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent

Your problem is more with TinyMCE than jQuery or Javascript specifically. If the above doesn't work you'll need to read the TinyMCE docs and/or API to figure out how to do what you want to achieve.


You can use this too.

tinyMCE.activeEditor.getContent();


TinyMCE provides a jQuery pluggin that you can use in conjunction with the onKeyUp command in the API

$('textarea.tinymce').tinymce({
    // Location of TinyMCE script
    script_url : 'path/to/tiny_mce.js',

    theme : "simple",

   setup : function(ed) {
      ed.onKeyUp.add(function(ed, l) {
          console.debug('Key up event: ' + e.keyCode);
   });
  }
});

There is also a preview plugin, which I suspect does what you were after more directly.


Your selector is wrong in the example above. It should be:

$('#thetextarea').live("keyup",function (e) {

not

$('thetextarea').live("keyup",function (e) {

Is this just a typo in your question?

If you're not sure which element has the text you need, you can try adding your event handler to both elements. Also I think you should be using $(this).val() vs. this.val:

$('#thetextarea, .mceContentBody').live("keyup",function (e) {
    var material = $(this).val();
    showPreview(material);
    return false;
});


tinymce.innerHTML gives me required result


//Html text editor text code

 <textarea name="description" class="tinymce" id="texteditor"></textarea>

You can print tinymce text editor code using java script like this.

var content = tinymce.get("texteditor").getContent();
alert(descriptionFieldTxt);

//output
[enter image description here][1]

[enter image description here][2]
//output with tags
enter image description here

//Now you can print tinymce texteditor value without p tag like this
var content = tinymce.get("texteditor").getContent({format: "text"});
alert(descriptionFieldTxt);

    enter code here

This code print text without tags
[enter image description here][3]


  [1]: https://i.stack.imgur.com/DgKYg.png
  [2]: https://i.stack.imgur.com/Tb1uf.png
  [3]: https://i.stack.imgur.com/Ahf60.png
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜