1

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>```
Crist
  • 13
  • 2
  • 1. What is `f` and `j`? 2. You're not using `document` in front of `getElementById`. 3. `"bleh"` isn't a valid id selector, it needs to be `#bleh`. 4. Divs don't have an `.value` property, use `.textContent` instead. 5. You're assigning `c` to the element, you need to assign the element's content to `c`. Please use your browser's debugging tools to see the errors in your code and then fix them accordingly – Nick Parsons Jan 18 '20 at 02:39

1 Answers1

0

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;

Rkv88 - Kanyan
  • 1,332
  • 2
  • 7
  • 15