How to remove extra spaces inside tag?

Hello! Not strong in JQuery, need urgent help!
There is such script sort alphabetically:

$(document).ready(function () {
 var $target = $('#layouts ol');
 var $elements = $('.author_poem');


 $elements.sort(function (a, b) {
 var an = $(a).text(),
 bn = $(b).text();

 if (an && bn) {
 return an.toUpperCase().localeCompare(bn.toUpperCase());
}

 return 0;
});
$elements.detach().appendTo($target);
});


and this form:



 <h2>Famous dynasty</h2>
<hr>
 <section id="layouts">
 <ol class="checklist" id="list">
 <div class="author_poem">
 <li class="author_name"><a href="akterskie_semy/agutin.html">(Here space)Agutin. Always was a leader</a></li>
</div>
 <div class="author_poem">
 <li class="author_name"><a href="akterskie_semy/ulickaya.htm">Aleksutin, V. A. Motivic complexes, embodying the theme of family in the stories of L. Ulitskaya</a> </li> 
</div>

</ol>
</section>


How do I remove the blank spaces before the title of the article and finalized the script or writing a new script, I tried using $.trim, but don't know how already display in the browser fixed option, then sort alphabetically
June 10th 19 at 16:01
1 answer
June 10th 19 at 16:03
Solution
$(document).ready(function () {
 var $target = $('#layouts ol');
 var $elements = $('.author_poem');


 $elements.sort(function (a, b) {
 var an = $.trim($(a).text()),
 bn = $.trim($(b).text());
 if (an && bn) {
 return an.toUpperCase().localeCompare(bn.toUpperCase());
}

 return 0;
});
$elements.detach().appendTo($target);
});

He figured out

Find more questions by tags jQueryJavaScript