简体   繁体   中英

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>```

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM