javascripthtmltextboxrespond-to

How to make a speicific text value respond with different text


I'm trying to get specific value back when I write "h" but it won't, and I don't know why. I have tried searching for the problem but could not find the solution, I don't know if I am just being dumb.

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>BOING</title>
    <script type="text/javascript">
    function res() {
      var a = getElementById("a").value;
      var c;

      if ( a == f ) {
        c = "Hello";
      }
      else if ( a == j) {
        c = "Hi";
      }
      c = document.querySelector("bleh").value;
    }
    </script>
  </head>
  <body class="bd" >
    <form class="form" action="index.html">
      <input id="a" type="text" name="h" pattern="Write Here" />
      <button id="b" type="button" name="button" onclick="res()">Ask</button>
    </form>
    <div class="blah" id="bleh"></div>
  </body>
</html>```

Solution

  • if you want to change text of DIV 'bleh' to respond to user when writing predefined message like Hello or Hi

    f="Hello";j="Hi";
         function res() {
              var a = document.getElementById("a").value;
              var c="";
    
              if ( a == f ) {
                c = "Hello";
              }
              else if ( a == j) {
                c = "Hi";
              }
             document.querySelector("#bleh").innerHTML=c;
            }
    

    https://jsfiddle.net/rkv88/kjuox029/11/

    modification:

    1 to put text into DIV element you must use .innerHTML property

    2 defined j & f

    you can use document.getElemntById("bleh") instead of document.querySelector("#bleh").innerHTML=c;