I am having javascript problem in my VF page so I am posting this question here.
Here is my code:
<html>
<input id="xyz" value=" "/>
<script>
alert( document.getElementById('xyz').value);
var x = document.getElementById('xyz').value;
if( x == ' ' )
alert(1)
else
alert(2);
</script>
</html>
The input field xyz is having the value as
I am checking the value in javascript but the if condition never evaluates to true. What could be the problem? jsfiddle
I am having javascript problem in my VF page so I am posting this question here.
Here is my code:
<html>
<input id="xyz" value=" "/>
<script>
alert( document.getElementById('xyz').value);
var x = document.getElementById('xyz').value;
if( x == ' ' )
alert(1)
else
alert(2);
</script>
</html>
The input field xyz is having the value as
I am checking the value in javascript but the if condition never evaluates to true. What could be the problem? jsfiddle
alert(x)
and find out?
– barak manos
Commented
Jan 15, 2014 at 9:36
The static String.fromCharCode()
method returns a string created by using the specified sequence of Unicode values.
For the non-breaking space, you can use String.fromCharCode(160)
if( x ==String.fromCharCode(160))
alert(1)
else
alert(2);
DEMO: http://jsfiddle/36eyp/
Ref: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
HTML:
<span id="xyz" > </span>
Javascript:
alert( document.getElementById('xyz').innerHTML);
var x = document.getElementById('xyz').innerHTML;
if( x == ' ' )
alert(1)
else
alert(2);
http://www.adamkoch./2009/07/25/white-space-and-character-160/
Use the UNICODE representation of the
HTML entity, \u00a0
:
if (x == "\u00a0")
alert(1);
will not the one obtained in function :
var x = document.getElementById('xyz').value;
if( x == '\xa0' ){
alert(1)
}else{
alert(2);
}
Fiddle : http://jsfiddle/NDBY3/1/
You could use regex as well. Probably not the best solution, but it works.
alert( document.getElementById('xyz').value)
var x = document.getElementById('xyz').value
if(/^\s$/.test(x))
alert(1)
else
alert(2)
See this fiddle
is the entity used to represent a non-breaking space. It is essentially a standard space, the primary difference being that a browser should not break (or wrap) a line of text at the point that this
occupies.