-2

i am trying to get part of a string. when I do this:

var str = document.getElementById("textbox").value;
var str = str.substring(30, 35);
console.log(str)

it dosnt work, and I get this error:

TypeError: Cannot read property 'substring' of undefined at /script.js:2:15

but when I do this:

var str = "aunchoflettersandstuffthatisdefinetlystringandnotsomethingelseweird"
var str = str.substring(30, 35);
console.log(str)

it works, and I get this:

finet

in the console.

its not length of the text, I have tried that before.

1 Answers1

0

you can assign an event listener to your textbox element, try this:

let textBox = document.getElementById("txtBox");
textBox.addEventListener('input', (e) => {
  var str = e.target.value.substring(5, 10);
  console.log(str)
})
<input type="textbox" id="txtBox">
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35