myTags.html
<div id="ControlWrapper">
<p></p>
<p><br></p>
<p></p>
</div>
myScripts.js
var a = $("#ControlWrapper").text();
if (a == ""){do something;}else {do something else;}
<div id="ctl00_PlaceHolderMain_txtContent__ControlWrapper_RichHtmlField" class="ms-rtestate-field" style="display: block;" aria-labelledby="ctl00_PlaceHolderMain_txtContent_label">
<p></p>
<p><br></p>
<p></p>
</div>
Hi, i have the above codes in my html and javascrpt files.
I want to get text from an element and save it in a variable then if it is empty it should return false in a conditional statement, but unfortunately it doesn’t work.
I need to comapre the text when there is no text inside an element.
3
Answers
You could try
if (!a) {...}
!a
givestrue
whena
isundefined
,null
or empty.If you’re in a browser you can use
console.log('[',a,']');
to see which valuea
has.This code exactly logs out "false value" as there is no text included inside the HTML node children elements.
In my opinion, we can use
trim()
(to remove spaces) method to specify a javascript conditional statement like this:myScripts.js and myDocument.html