I am trying to type a text in a conteneditable div and when I loop through them the for loop as an array and check if there are any white space characters (i.e abc de
i.e space between the two words it does not recognize them as empty string ). Please see below code.
Script:
function htmltoBBcode() {
$("#hidden").html($('#textEditor').html());
$("#hidden").html($('#textEditor').html());
var arr=$("#hidden").text();
for(var i=0;i<arr.length;i++)
{
if(arr[i]=='')
{
console.log("i is true"+arr[i]);
}
else{
console.log("i is false"+arr[i]);
}
}
}
In the above code, I am taking the text from the contenteditable div and looping through them, if the arr[i]==''
it should return true
in the console.log
but it keeps returning false always, even though it is an empty string(white space).
Demo
I am trying to type a text in a conteneditable div and when I loop through them the for loop as an array and check if there are any white space characters (i.e abc de
i.e space between the two words it does not recognize them as empty string ). Please see below code.
Script:
function htmltoBBcode() {
$("#hidden").html($('#textEditor').html());
$("#hidden").html($('#textEditor').html());
var arr=$("#hidden").text();
for(var i=0;i<arr.length;i++)
{
if(arr[i]=='')
{
console.log("i is true"+arr[i]);
}
else{
console.log("i is false"+arr[i]);
}
}
}
In the above code, I am taking the text from the contenteditable div and looping through them, if the arr[i]==''
it should return true
in the console.log
but it keeps returning false always, even though it is an empty string(white space).
Demo
Share Improve this question edited Nov 21, 2015 at 16:54 Pbk1303 asked Nov 21, 2015 at 16:32 Pbk1303Pbk1303 3,8023 gold badges33 silver badges47 bronze badges 5-
1
A space isn't an empty string.Quite unclear what you are expecting. Maybe
arr[i]==' '
??? – A. Wolff Commented Nov 21, 2015 at 16:34 -
@A.Wolff: when i get the text
abc de
and display it character by character in an array . what will the space be stored as?. I tried getting the typeof arr[i] and it showed the space as string. am i wrong? – Pbk1303 Commented Nov 21, 2015 at 16:37 - '' == " "; //false always. An empty string cannot be equal to a space character. – Akshay Khandelwal Commented Nov 21, 2015 at 16:38
-
@Pbk1303 I still don't understand what you want/mean. You want to ignore spaces or what? For 'empty characters' ( do you mean white space characters???), use a regex:
/\s/.test(arr[i])
– A. Wolff Commented Nov 21, 2015 at 16:39 - @A.Wolff: thank you, yes your right i was trying to look for white space character. – Pbk1303 Commented Nov 21, 2015 at 16:45
3 Answers
Reset to default 4If you want to check if a string is empty use:
if (!arr[i]) {
// is emtpy
}
Take into consideration that the String is not considered empty if it has whitespace in it! If you want to ignore the whitespace:
if (!arr[i].trim()) {
// is empty
}
There is not an empty carácter un this position. It's a space carácter. You can check if there space characters by using thisfunction.
function hasWhiteSpace(s) {
return /\s/g.test(s);
}
if(hasWhiteSpace(arr[i]))
{
console.log("i is true"+arr[i]);
}
As A.Wolff pointed out, a space is not an empty string. I also changed ==
to ===
to avoid coercion. Here's a jsbin
js
function htmltoBBcode() {
$("#hidden").html($('#textEditor').html());
$("#hidden").html($('#textEditor').html());
var arr=$("#hidden").text();
for(var i=0;i<arr.length;i++)
{
if(arr[i]===" ")
{
console.log("i is true: "+arr[i]);
}
else{
console.log("i is false: "+arr[i]);
}
}
}