tags:

views:

1672

answers:

7

I want to put a function call in my pages called enableTinyMCE(); In that function I want to see if there are any textarea's in my page and if so do the tinyMCE.init() function. How do I detect if there are any textarea elements in the page?

+5  A: 
if($('textarea').length > 0) {
    document.write('we have at least one textarea');
}

OR

if($('textarea').length) {
    document.write('we have at least one textarea');
}

Read this, from the FAQ:

http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_test_whether_an_element_exists.3F

karim79
+1  A: 

A jQuery selector always returns an array even if no matching elements are found. This means that you need to check the length. Try this instead.

if($('textarea').length > 0) {    
 document.write('we have at least one textarea');
}
ichiban
+4  A: 
if( $('textarea').length > 0 ) {
    ...
}
Mario Menger
A: 

I have done the following as suggested however I still only want to call enableTinyMCE if there are textareas in my page. I cannot call this function in document.ready. See Google! Any ideas?

$(function() {

     if ($('textarea').length > 0)
    {
       var data = $('textarea');
       $.each(data, function(i)
       {
         tinyMCE.execCommand('mceAddControl', false, data[i].id);
       }
       );
    }

});

function enableTinyMCE()
{
        tinyMCE.init({
            plugins: 'paste',
            theme_advanced_toolbar_location: 'top',
            theme_advanced_buttons1: 'pastetext,pasteword,copy,cut,separator,bold,italic,underline,separator,bullist,numlist,separator,undo,redo,separator,link,unlink,separator,charmap,separator,formatselect,separator,code',
            theme_advanced_buttons2: '',
            theme_advanced_buttons3: '',
            mode: 'textareas',
            theme: 'advanced',
            theme_advanced_blockformats: 'None=p,Heading 3=h3,Heading 2=h2'
        });
}

the tinymce.init needs to be called outside of documentready but $('textarea').length is always zero outside of documentready. Help!

Jon
why does the tinymce need to be called outside of document.ready? the document.ready is to make sure the DOM is registered by the browser
TStamper
I have found this - http://www.latenightpc.com/blog/archives/2008/06/09/setting-up-tinymce-with-jquery-and-cakephp-12 but I am doing it dynamically so I don't know the id of the textareas. Also what if you have more than one textarea
Jon
the jquery selector that you're using ($('textarea') doesn't look for id. It looks for all tags that textarea
TStamper
read the article. i need to pass an id to the tinymce command
Jon
I have never used TinyMCE before, but since it looks like its a javascript function, there is nothing wrong with having it in the document.ready bracket. As far as the textarea situation, if you're having it check outside the document.ready then it will return 0 because it looks for textarea tags before waiting til the document has loaded therefore; it will not find any unless the textarea tag is before you check, which is why it should be in document.ready to make sure all tags all loaded
TStamper
+3  A: 

To expand on karim79 answer.

From the jQuery docs page: Note: It isn't always necessary to test whether an element exists. The following code would show the item if it exists, and do nothing (no errors) if it did not.

Which that means is that you can just do:

$(function(){
    $("textarea").each(function(i){
     this.enableTinyMCE();
    })
})

Edit:

There is actually a jQuery plugin being developed for this purpose. I would download and try out the plugin and contribute to it's development if you can.

jq-tinyMCE

rip747
A: 

This is my solution

if ($('textarea').length > 0)
    {
       var data = $('textarea');
       $.each(data, function(i)
       {
         tinyMCE.execCommand('mceAddControl', false, data[i].id);
       }
       );

       $('form').bind('form-pre-serialize', function(e) {
            tinyMCE.triggerSave(true,true);
        });
    }

And to enable tinyMCE I have done this

<% if (ViewData["TextAreaVisible"] != null && bool.Parse(ViewData["TextAreaVisible"].ToString()) == true)
   {%>
        <script type="text/javascript" src="../../Scripts/tinymce/tiny_mce.js"></script>
        <script type="text/javascript">
           enableTinyMCE();
        </script>
<%} %>

EnableTinyMCE does this

function enableTinyMCE() {

    tinyMCE.init({
        plugins: 'paste',
        theme_advanced_toolbar_location: 'top',
        theme_advanced_buttons1: 'pastetext,pasteword,copy,cut,separator,bold,italic,underline,separator,bullist,numlist,separator,undo,redo,separator,link,unlink,separator,charmap,separator,formatselect,separator,code',
        theme_advanced_buttons2: '',
        theme_advanced_buttons3: '',
        mode: 'none',
        theme: 'advanced',
        theme_advanced_blockformats: 'None=p,Heading 3=h3,Heading 2=h2'
    });

}

Jon
+1  A: 

It is worth noting that the functionality you are looking for can be handled by TinyMCE itself:

If you set the mode parameter to textareas in your tinyMCE.init() call, it will automatically convert any textareas it finds to editor instances. If there's no textareas, it won't do anything, quietly.

tinyMCE.init({
    ...
    mode : "textareas",
    ...
});

Conversely, you could tell TinyMCE to convert only textareas that match a CSS class name by using the specific_textareas value to the mode parameter.

tinyMCE.init({
   ...
   mode : "specific_textareas",
   editor_selector : "mceEditor"
});

http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/mode

jason