Reputation: 93
I have a slight JQuery issue that I am struggling with. The Codepen I have created is a script where if a tab has no content then it removes the corresponding tab heading. The code below is working great as long as the .tab-content div has no spaces, if a space is inside of that div then the code breaks and the tab re-appears.
Here is the code:
HTML
<div class="tab-boxes cf">
<ul class="tabs" data-persist="true">
<li class="tab1">
<a href="#view1">Tab 1</a>
</li><!--li .tab1-->
<li class="tab2">
<a href="#view2">Tab 2</a>
</li><!--li .tab2-->
<li class="tab3">
<a href="#view3">Tab 3</a>
</li><!--li .tab3-->
</ul><!--ul .tabs-->
<div class="tabcontents">
<div id="view1">
<div class="tab-content">I am Tab One</div>
</div><!--#view1-->
<div id="view2">
<div class="tab-content">Hello, I am Tab Two</div>
</div><!--#view2-->
<div id="view3">
<div class="tab-content"> </div>
</div><!--#view3-->
</div><!--.tabcontents-->
</div><!--.tab-boxes-->
JQuery:
var $j = jQuery;
$j(document).ready(function(){
$j('.tab-content').each(function(i){
if(!$j(this).text().length) $j('.tabs li').eq(i).hide();
})
});
Here is my current Codepen link:
http://codepen.io/nickelse/pen/NqOdrM
Cheers,
Nick
Upvotes: 0
Views: 34
Reputation: 4507
You can use .trim()
to remove whitespace from the beginning and ending of a string.
http://api.jquery.com/jQuery.trim/#str
So I guess you could e.g. do
if (! $.trim( $(this).text() ).length )
or
if (! $(this).text().trim().length )
Upvotes: 1