Search code examples
javascriptasciifromcharcode

Need to output image depending on what letter is typed into input box in javascript using fromCharCode, any ideas?


Okay so I'm working on a project where I have to output a cat image that resembles a letter in the alphabet depending on what letter the user types into the input box. So for example if a is typed in then a image of a cat that looks like an a will be displayed. I have to implement fromcharcode into my javascript, but I'm not sure how to actually go about doing this. Any help would be great. Thanks.


Solution

  • You need this

    function myfun() {
      var text = document.getElementById("tb").value;
      for (var i = 0; i < text.length; i++) {
        var letter = text.charAt(i);
        var img = document.createElement("img");
        var att = document.createAttribute("src");
        att.value =  "cat_"+letter+".jpg";
        img.setAttributeNode(att);
        document.body.appendChild(img);
      }
    }
    <input type="text" id="tb">
    <button onClick="myfun()">Get Image</button>