WordPress.org

Forums

[resolved] Update TinyMCE instance with AJAX not working (2 posts)

  1. Christiaan Conover
    Member
    Posted 1 year ago #

    I'm trying to update a TinyMCE instance I created with wp_editor() in my plugin using the following JavaScript:

    jQuery( document ).ready( function( $ ) { // Don't execute anything until the page is loaded
    	$( "#cc_author_postauthor" ).live( "change", function() { // If the author dropdown value is changed, execute the script
    		/* Data to pass to the server. Called below during $.post() */
    		var data = {
    			action : 'cc_author_change_postauthor',				// Action hook for the server-side callback
    			nonce : authorchange.nonce,							// Nonce received from server to authenticate request
    			authorID : $( "#cc_author_postauthor" ).val()		// author ID for retrieving profile data
    		};
    
    		/* Send request to the server and process response */
    		$.post(
    			authorchange.ajaxurl,								// URL to send the request to the server
    			data,												// Data to send to the server, stored in var data
    			function( jsonString ) {							// Script to execute upon successful response from server
    				var authormeta = $.parseJSON( jsonString );		// Parse the JSON received from the server response
    
    				console.log( "This is the JSON response from the server: " + jsonString );
    
    				$( "#cc_author_meta\\[0\\]\\[display_name\\]" ).val( authormeta.display_name );	// Change the value of the author display name to the value received from the server
    
    				/* Handle description update differently depending on whether 'wysiwyg' is enabled in plugin options */
    				if ( authormeta.wysiwyg == 'yes' ) {
    					tinyMCE.get( "cc_author_meta\\[0\\]\\[description\\]" ).setContent( authormeta.description );
    					console.log( "WYSIWYG is enabled and update attempted" );
    				}
    				else {
    					$( "#cc_author_meta\\[0\\]\\[description\\]" ).val( authormeta.description );	// Change the value of the author bio to the value received from the server
    				}
    			} // function( jsonString )
    		); // $.post
    	}); // $( "#cc_author_postauthor" ).change()
    }); // jQuery ready check

    The console reports that the variable I'm checking for does have a value of 'yes', so the code should execute. However, the console also reports the following error:

    Uncaught TypeError: Cannot call method 'setContent' of undefined

    Does WordPress not use the object tinyMCE when an instance is created by wp_editor()? Is my syntax wrong?

  2. Christiaan Conover
    Member
    Posted 1 year ago #

    I figured it out. This line:

    tinyMCE.get( "cc_author_meta\\[0\\]\\[description\\]" ).setContent( authormeta.description );

    Needed to be changed to this:

    tinyMCE.get( "cc_author_meta[0][description]" ).setContent( authormeta.description );

    The escape characters from my jQuery code was messing up the tinymce call.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags