tinymce textareas value error – cant solve-Collection of common programming errors

$('.button-save').click(function(b){
                b.preventDefault();
                var title = $('#title').val(),
                    category = $('#category').val(),
                    file = $('#file').val(),
                    short_content = tinyMCE.get('#short_content').getContent(),
                    content = tinyMCE.get('#content').getContent(),
                    date = $('#date').val(),
                    language = $('#language').val();
                alert(short_content);
                $.ajax({
                    type: 'POST',
                    url: '/admin/save',
                    data: {
                        title:title,
                        category:category,
                        file:file,
                        short_content:short_content,
                        content:content,
                        date:date,
                        language:language
                    },
                    success: function(data){
                        $('.adm-notification').html(data);
                    }
                });
            });

As you see there are two textareas (short_content,content); I tried to get them via tinyMCE.get(‘#content’).getContent(). But i’m getting the error

Uncaught TypeError: Cannot call method ‘getContent’ of undefined

HTML code

 
            
                Page title (required)
                
            
            
                Category (required)
                
            
            
                Attachments
                
            
            
                Short content (required)
                ".$edit['short_content']."
            
            
                Long content (required)
                ".$edit['content']."
            
            
                Date (required)
                
            
            
                Language (required)
                
            
            
                Save page
            
        

How to fix?

  1. You need to supply the id of the textarea, without the #.

    tinyMCE.get('long_content').getContent()
    tinyMCE.get('short_content').getContent()
    

    btw, you need to decide whether you want to submit the form or read out the value with JavaScript and then send it to the server. Here a solution reading it out with JS:

    
        tinyMCE.init({
                mode : "textareas"
        });
    
    
        $(function(){
            $('#button-save').click(function(){
                console.log("ok");
                var short_content = tinyMCE.get('short_content').getContent();
                var content = tinyMCE.get('long_content').getContent();
                alert(short_content);
                alert(content);
            });
        });
    
    
    
    'short_content'
    'content'
    
    Save page
    

    Same as a working fiddle.

Originally posted 2013-11-15 09:06:58.