javascript - Preventing blank input not working -


i'm trying prevent blank input on editable list item, i'm using same code use somewhere else reason not working.

i have:

if(foldername == "" || foldername == " " || foldername.charat(0) == " "){         $(this).remove();     } 

it removing item if not type anything, though if enter blank space doesn't call remove.

strange thing using exact same code in different area , it's working fine.

here full function:

$(document).on('focusout', '#folders li', function(){ //add class when lose focus/shorten name if long     var foldername = $(this).text();     $('#folders li').removeattr('contenteditable');     $('#folders li').removeclass('active-tab');     $(this).addclass('active-tab');     if(foldername.length > 15){         $(this).attr('title', $(this).text());         shortfoldername=foldername.substring(0,15) + '...';         $(this).text(shortfoldername);     }     else if(foldername.length <= 15){         $(this).text(foldername);         $(this).attr('title', $(this).text());     }     if(foldername == "" || foldername == " " || foldername.charat(0) == " "){         $(this).remove();     }     console.log(foldername); }) 

html:

<div id="tabs">     <ul id="folders">         <li class="active-tab" >all notes</li>         <li>work</li>         <li>personal</li>         <li id="add" class="glyphicon glyphicon-plus"></li>     <ul> </div> 

you can use .trim() function , check string's length.

if(foldername == "" || foldername.trim().length === 0){     $(this).remove(); } 

note: trim() function removes whitespace both ends of string.

mdn


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -