You need to wait for the widget control to be created before you can attempt to . You can do this by listening for the add
event on wp.customize.control
. Here’s some example code that will do what you want:
( function( api ) {
function handleTextWidgetControl( control ) {
if ( ! control.extended( api.Widgets.WidgetControl ) ) {
return;
}
if ( 'text' !== control.params.widget_id_base ) {
return;
}
/*
* Make sure the widget's contents are embedded; normally this is done
* when the control is expanded, for DOM performance reasons.
*/
control.embedWidgetContent();
// Now we know for sure the widget is fully embedded.
console.info( control.container.find( 'textarea' ).attr( 'id' ) );
}
api.control.each( handleTextWidgetControl );
api.control.bind( 'add', handleTextWidgetControl );
})( wp.customize );