Hayi
Hayi

Reputation: 6236

Get value of textarea with this element and not identifier

I want to check empty textarea in tinymce

$(".tooltip-empty-editor").each(function() {        

    if( $.trim( tinymce.get(this).getContent() ) == '' ) { 
        // .....
    }

});

html

<textarea class="editor tooltip-empty-editor " name="modif" >....</textarea>

But this i got error tinymce.get(this).getContent() with this he expect a ID.

How can I use this with tinymce to get value of textarea ?

Upvotes: 1

Views: 517

Answers (3)

Michael Fromin
Michael Fromin

Reputation: 13746

Per the product documentation, the get() API for TinyMCE "Returns an editor instance by id". The id parameter is expected to be a String. In your example you seems to be trying to pass it the "this" object which is not a String.

I would give each textarea on the page a unique ID attribute and you can then reference each textarea using that ID.

As one other poster noted you can also use an integer to access each TinyMCE instance on the page (e.g. tinymce.get(0) and tinymce.get(1))

Upvotes: 1

Tim Vermaelen
Tim Vermaelen

Reputation: 7059

Depending on how your textarea's are related to eachother, you could try to find the index. You need a sibling, or if they're all children of the same node (parent) no need to bother => $(this).index()

If you only have one on the page => textarea = tinymce.get(0)

HTML expectation:

<div class="parent">
    <div class="sibling">
        <textarea class="editor tooltip-empty-editor " name="modif" >....</textarea>
    </div>
    <div class="sibling">
        <textarea class="editor tooltip-empty-editor " name="modif" >....</textarea>
    </div>
</div>

JS

$(".tooltip-empty-editor").each(function() {
    var index = $(this).closest('.sibling').index(),
        //index = $(this).index(),
        textarea = tinymce.get(index); // get() requires an id or a number

    if( $.trim( textarea.getContent() ) == '' ) {
        // .....
    }
});

If your html is polluted with other elements on the parent, you'll need to use .filter() to get the index from.

Upvotes: 1

Yahya
Yahya

Reputation: 3444

Why can't you define the id and access it as follows?

$(".tooltip-empty-editor").each(function() {        

    if( $.trim( tinymce.get($(this).attr("id")).getContent() ) == '' ) { 
        // .....
    }

});

Upvotes: 1

Related Questions