Q:

Change an image when input is given

I have a small basic html code with JavaScript . All I want is when I type 3,it should show me an image as specified and for 9 accordingly. This is the demo. Visit http://jsbin.com/UVOFeGIG/1/edit
Its working there. I don't know why the JavaScript doesn't work this way. Can anyone figure this out?
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<script type="text/javascript">

var num2img = {
  "3":"visa",
  "9":"mastercard"
};

$('#num').on('input', function(){
  var val = this.value;
  if(val.length<=1){
     var n = this.value.charAt(0);
     if(val && num2img[n]!==undefined){
       $('#cardImage')[0].src = 'http://placehold.it/100x100/eee&text='+ num2img[n] +'.png';
     }else{
       $('#cardImage')[0].src = 'http://placehold.it/100x100/cf5';
     }
  }
});

</script>
<meta charset=utf-8 />
<title>Demo by Roko C.B.</title>
</head>
<body>

  <input id="num"><br>
  <img id="cardImage" src="http://placehold.it/100x100/cf5">

</body>
</html>

A:

Try this 
$(document).ready(function(){

var num2img = {
  "3":"visa",
  "9":"mastercard"
};

    $('#num').keyup(function(){
      var val = this.value;
      if(val.length<=1){
         var n = this.value.charAt(0);
         if(val && num2img[n]!==undefined){
           $('#cardImage')[0].src = 'http://placehold.it/100x100/eee&text='+ num2img[n] +'.png';
         }else{
           $('#cardImage')[0].src = 'http://placehold.it/100x100/cf5';
         }
      }
    });
});

DEMO

