Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input field value from dialog box in TinyMCE

all.

I'm having a hard time figuring this out, it's the second time I need to do something with tinyMCE but this time I just can't find the answer.

Here's what I want to do: I have added a button on my editor that opens a new popup with a single text input field and a button. I want to click the button and grab the value I set in my input field, then use that value to modify what I have in my editor.

Here's what I have so far - only relevant code:

    init : function( ed, url ) {
        ed.addCommand( 'mceTooltip', function() {
            ed.windowManager.open({
                file: 'imageurl.html',
                width: 480,
                height: 180,
                inline: 1,
                title: 'Please enter an image URL'
            }, {});
        });
    }

This is what imageurl.html has:

<input type="text" id="image-url" />
<input type="button" id="submit-image-url" value="Ok" />

So, what I need to do is get whatever "image-url" text input has whenever I click the OK button, and use that text inside my editor. I know I can use ed.selection.setContent( fieldValue ) and it will replace my selected text with the image-url value, I just don't know how to get the image-url value.

The most detailed info I was able to find was http://www.tinymce.com/wiki.php/How-to_implement_a_custom_file_browser but I can't make it work for my needs. Anybody who can help me out with this? I'm sure it should be simple for somebody with more experience on this.

Thank you all for your attention.

Updated imageurl.html **

        <script>
            document.getElementById( 'submit-image-url' ).onclick = function(){
                var imageUrl = document.getElementById( 'image-url' ).value;

                window.parent.tinyMCE.activeEditor.execCommand( 'mceInsertContent', 0, imageUrl );
                window.parent.tinyMCEPopup.close(); // this line gets me this error: "Uncaught TypeError: Cannot read property 'windowManager' of undefined "
            };
        </script>
like image 963
andrux Avatar asked Apr 22 '13 20:04

andrux


People also ask

How do you get data from TinyMCE text editor?

You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.


2 Answers

Ok, this shouldn't be that difficult.

issue this in a script-tag on bottom of your imageurl.html or use a document ready javascript function. The following will add an onclick handler to your button which will get the image_url and write this to the tinymces selection.

$('#submit-image-url').bind('click', function(){
    var image_url = $('#image-url').val();

    // in case you are using a real popup window
    window.opener.tinymce.activeEditor.selection.setContent(image_url);

    // in case you use a modal dialog
    tinymce.activeEditor.selection.setContent(image_url);
});
like image 85
Thariama Avatar answered Oct 19 '22 03:10

Thariama


You have opened up a window so you are currently in IFrame having imageurl.html ok..

what you have to do is

on a parent page create one variable of global type like

var image_url = "";

now on imageurl page create a script on body part like this

<body>
<script>
$("#button").click(function(){
window.parent.image_url = $('image-url').val();
});
</script>
</body>

make sure you have jquery on imgurl. or otherwise use addeventlistener or attachevent method

logic is get parent window's element from the iframe and save it from iframe.

like image 27
Vishal Sharma Avatar answered Oct 19 '22 03:10

Vishal Sharma