sadada dadasdad sadada dadasdad - 3 years ago 199
HTML Question

How to check if contenteditable div contain only spaces jquery

I want to prevent users from submit posts that contain only spaces so i try something like trim but i failed.

How can detect if contenteditable div contain only spaces?

if(text.replace(/\n/g, "") =="" || jQuery.trim(text) == 0) {
var is_empty = 'true';
}

Answer Source

document.getElementById("shit").addEventListener("click",function() {
    var is_empty = document.getElementById("contentEditable").innerText.trim() == ""
    document.getElementById("result").innerHTML = is_empty
})
<div id="contentEditable" contenteditable="true">I am not empty</div>
<input type="button" id="shit" value="check"><p id="result"></p>

as of what Hamid Mosalla says, trimming the innerText of the content editable will solve your problem. https://jsfiddle.net/8Lzrcc2n/1/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download