1. <div id="div_Msg"> Test the div </div> 2. <div id="div_Msg"> </div> In the first instance there is the text in the div. In the second instance there is no text. Using javascript how can it be tested if a div has text in it.
If you are using jQuery, you can do it like this:
if($.trim($('#div_Msg').text()) != "") { // Code here } In just plain JavaScript, do this:
if(document.getElementById("div_Msg").innerHTML.replace(/^\s*/, "").replace(/\s*$/, "") != "") { // Code here } Both cases get the text and trim whitespace off the beginning and end of the string, then compare it to an empty string.
If using jQuery, you can do:
$.trim($('#div_Msg').text()); Note: elements cannot have the same id
$.trim() that value before checking against an empty string: if ($.trim($('#div_Msg').text()) != '') { ..