Firstly you need to precisely define what the requirements are.
In your code you test against undefined
jQuerys implementation of html never returns undefined
so there's no reason to test against that. However there might or might not be reason to test against null
<div id="spanSelectedLayout">content</div>
var txt = $("#spanSelectedLayout").html() //txt will be "content"
<div id="spanSelectedLayout"> </div>
var txt = $("#spanSelectedLayout").html() //txt will be return " "
<div id="spanSelectedLayout"> </div>
var txt = $.trim($("#spanSelectedLayout").html()) //txt will be return ""
<div id="spanSelectedLayout"> </div>
var txt = $("#spnSelectedLayout").html() //txt will be null
The latter is most likely to occur due to miss-spelling a selector, Ie a bug so you probably should treat that differently than "" or an all whitespace string. but "" and all whitespace HTML is semantically the same so you should probably treat those values alike which might then leading to
var selectedLayOutIdx=$.trim($("#spanSelectedLayout").html());
if( selectedLayOutIdx == "" ) {
//is empty
}
However $.trim(null)
returns ""
and not null so that will still hide the bug and you will therefor have to decide between the more concise code or something like this
var selectedLayOutIdx=$("#spanSelectedLayout").html();
if(selectedLayOutIdx == null) { throw "Invalid selector" }
if( && $.trim(selectedLayOutIdx) == "" ) {
//is empty
}