2

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

0

3 Answers 3

2

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>

Sign up to request clarification or add additional context in comments.

Comments

2

You're trying to call charAt() on element instead you should call it on the value of element so try to add .value, replace :

var x = document.getElementById("hash_input");

By :

var x = document.getElementById("hash_input").value;

NOTE : Val + val1; should be Val + Val1; (uppercase the v at the beginning of Val1), also remove extra } at the end.

Hope this helps.


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;
}
<div class="hash_ctrl">
  <textarea name="hash_input" id="hash_input">abcdef</textarea>
</div>
<button onclick="myFunction()">Execute my function</button>

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

Comments

0

You'll want to use the textaras value property instead of innerHTML (assuming you want to use what was typed in the textarea).

Also-no reason that I can think of to use chartAt method. Strings are indexable like arrays.

    var a="value";  
    var b=a[2];  // "l"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.