1

Suppose I have button like this:

<button>&divide;</button>

I want to use switch operator with string variable.

<!DOCTYPE html> 
<html lang="en">
<head>      
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>     
    <script type="text/javascript">

$(document).ready(function()
{  
  $("button").click(function(){
      var value=$(this).text();
      console.log(value);
    switch(value){

      case "÷":
         alert("OK");
         break;
      case "&divide;":
         alert("OK");
         break;
    }
  });   

}
);

    </script>   
</head>
<body>
<button>&divide;</button>
</body>
</html>

But none of those alerts appear on button click.console.log() shows:

"÷"

My browser - firefox 39.0.

kurumkan
  • 2,635
  • 3
  • 31
  • 55

1 Answers1

1

Consider that Html Node Text isn't always a safe source of truth... A good choise could be putting some attribute that gives you the information that you need and use views only for data presentation...

function CheckBtnTextCtrl($) {
  let 
    $btns = $('button')
  ;
  
  function smartTextSanitization(text) {
    let decoder = document.createElement('textarea');
    decoder.innerHTML = text;
    
    return decoder.value;
  }
  
  function onBtnClick(event) {
    let 
      $btn = $(this),
      $btnText = $btn.text(),
      text = smartTextSanitization($btnText)
    ;
    
    switch(text) {
      case "÷":      
        console.log('it is division');
        break;
    }
  }
  
  $btns.click(onBtnClick);
}

jQuery(document).ready(CheckBtnTextCtrl);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>&divide;</button>

Good way (in my opinion):

function CheckBtnCtrl($) {
  const ACTION_TYPES = {
    "SUM": 1,
    "DIVIDE": 2
  };
  
  let $btns = $('button');
  
  function onBtnClick(event) {
    let 
      $btn = $(this),
      action = $btn.data('actiontype')
    ;
    
    switch(action) {
      case ACTION_TYPES.SUM:
        console.log('ok, we need to sum values');
        break;
        
      case ACTION_TYPES.DIVIDE:
        console.log('ok, we need to divide values');
        break;
    }
  }
  
  $btns.click(onBtnClick);
}
jQuery(document).ready(CheckBtnCtrl);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-actiontype="2">&divide;</button>
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • 1
    Might be a bit premature to be using `const` and `let` in examples, considering few mobile browsers support them: http://kangax.github.io/compat-table/es6/#test-let – Heretic Monkey Aug 04 '16 at 18:18
  • I support your *alternative* method of using a [data attribute](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes). But, can you tell what's causing the OP's specific problem? – showdev Aug 04 '16 at 18:19
  • 1
    The problem resides on the HTML Entities, as I said, they are not a safe source of truth... The example works only when you sanitize them (my first example), because you decode that HTML ENTITY. (when you query on dom... it can return an unicode, html entities, or something else depending on browser)... – Hitmands Aug 04 '16 at 18:21
  • 1
    Thank you. Very interesting info. But where can I find more about such unreliability?Please, if you able, provide me some resources about it. It would be very kind of you – kurumkan Aug 04 '16 at 18:43