Search code examples
javascriptcharat

JavaScript using CharAt get from input text


I have got textarea i'm going to use CharAt to get characters 3 and 5..

Textarea

<div class="hash_ctrl"><textarea name="hash_input" id="hash_input"></textarea></div>

Button button onclick="myFunction()"

<p id="demo"></p>

<script>
    function myFunction() {
        var x = document.getElementById("hash_input");
        var Val = x.charAt(3);
        var Val1 = x.charAt(5);

        document.getElementById("demo").innerHTML = Val + val1;
    }

</script> 

The result is nothing ...

Any help appreciate ...


Solution

  • Use document.getElementById("hash_input").value to get the value. .value will retrieve value property from the input element.

    Try this:

    function myFunction() {
      var x = document.getElementById("hash_input").value;
      var Val = x.charAt(3);
      var Val1 = x.charAt(5);
      document.getElementById("demo").innerHTML = Val + Val1;
      //________________________________________________^^^^ typo here
    }
    <div class="hash_ctrl">
      <textarea name="hash_input" id="hash_input">HelLoO</textarea>
    </div>
    <button onclick="myFunction()">Test</button>
    <p id="demo"></p>