0

Could you share the solution of substring methods from the input string using HTML+Javascript ?

<!DOCTYPE html>
<html>
<body>
<input type="text" value="test"></input>
<button onclick="myFunction()" id="testCode">Click</button>
<script>
function myFunction() {
  document.getElementById("testCode");
  var res = substring(0, 5).value;
  document.write(res);}
</script>

</body>
</html>

2 Answers 2

2

Substring will return part of the string. Also, add the ID on input field to get the value but rather using the button id itself. See the following snippet

function myFunction() {
  const test = document.getElementById("testInput");
  var res = test.value.substr(0, 3);
  
  document.write(res);
  
  }
<!DOCTYPE html>
<html>
<body>
<input type="text" value="test" id="testInput"></input>
<button onclick="myFunction()" id="testCode">Click</button>

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

Comments

1

substring should be called on string variable

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

Like so

const test = document.getElementById('your-id')
test.value = test.value.substring(0, 6);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.