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.
try something like this
$(function(){
// you have not given any event
$('#num').on('keyup', function(){
var val = this.value;
if(val.length){//your length checking logically wrong
var n = this.value.charAt(0);
if(val && num2img[n]!==undefined){
$('#cardImage')[0].src = 'data/1357696142_mastercard1'+ num2img[n] +'.gif';
}else{
$('#cardImage')[0].src = 'http://placehold.it/100x100/cf5';
}
}
});
})