jquery-notebook icon indicating copy to clipboard operation
jquery-notebook copied to clipboard

Method to check if notebook is empty

Open ghost opened this issue 10 years ago • 3 comments

I want to request some new feature.

Currently I can't find a way to check if notebook is empty.

                if (/^\s*$/.test($(this).text())) {

                } else {

                }

You are using this to set placeholder, but this is not enough for checking content because It can return Your text here...

this is how I'm checking inside my function. note whitespaces

                if (/^\s*$/.test(form.find('.jquery-notebook').text()) || form.find('.jquery-notebook').text() == ' Your text here... ') {
                    alert(1)
                } else {
                    alert(2)
                }

probably this is better to be done with test but I don't know anything about regEx

also I'm writing too much issues here, So I'll merge another feature request here and you decide if this needs to be implemented. What about disabling notebook. You know, sometimes it takes time for ajax to complete request (especially when uploading images parallely) so usually one would disable textarea and upon complete reenable it.

ghost avatar Mar 05 '14 20:03 ghost

The trick here is to check if either the .placeholder element exists or if the editor itself is empty.

Consider this code:

    $.fn.isEmpty = function() {
        if (($(this).find('.placeholder').length > 0) || (/^\s*$/.test($(this).text())) ) {
             return true;
        } else {
             return false;
        };
    };

Simple use of the function:

Add to HTML:

<a id="test" href="#">Test if empty</a>

Add to $(document).ready

$('#test').click( function() {
    if($('.editor').isEmpty()) {
        console.log("Empty");
    } else {
        console.log("Not empty");
    };
});

krilor avatar Mar 16 '14 19:03 krilor

Thanks, this makes sense

ghost avatar Mar 16 '14 19:03 ghost

@krilor, are you planning to issue a pull request for the changes you made, or are you keeping them local to your repository?

timcosta avatar May 20 '14 19:05 timcosta