最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

  not working with javascript if condition - Stack Overflow

programmeradmin1浏览0评论

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="&nbsp;"/>
    <script>
        alert( document.getElementById('xyz').value);
        var x = document.getElementById('xyz').value;
        if(  x == '&nbsp;' )
           alert(1)
        else
           alert(2);
    </script>
</html>

The input field xyz is having the value as &nbsp; 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="&nbsp;"/>
    <script>
        alert( document.getElementById('xyz').value);
        var x = document.getElementById('xyz').value;
        if(  x == '&nbsp;' )
           alert(1)
        else
           alert(2);
    </script>
</html>

The input field xyz is having the value as &nbsp; I am checking the value in javascript but the if condition never evaluates to true. What could be the problem? jsfiddle

Share Improve this question edited Jan 15, 2014 at 9:35 Rico Sonntag 1,5161 gold badge15 silver badges22 bronze badges asked Jan 15, 2014 at 9:32 Shebin MathewShebin Mathew 3181 gold badge7 silver badges19 bronze badges 2
  • Why don't you do alert(x) and find out? – barak manos Commented Jan 15, 2014 at 9:36
  • tested and returns space – Shebin Mathew Commented Jan 15, 2014 at 9:37
Add a ment  | 

5 Answers 5

Reset to default 4

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" >&nbsp;</span>

Javascript:

    alert( document.getElementById('xyz').innerHTML);
    var x = document.getElementById('xyz').innerHTML;
    if(  x == '&nbsp;' )
       alert(1)
    else
       alert(2);

http://www.adamkoch./2009/07/25/white-space-and-character-160/

Use the UNICODE representation of the &nbsp; HTML entity, \u00a0:

if (x == "\u00a0")
    alert(1);

&nbsp; 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

&nbsp; 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 &nbsp; occupies.

发布评论

评论列表(0)

  1. 暂无评论