Reputation: 29
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>
Upvotes: 0
Views: 453
Reputation: 4302
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>
Upvotes: 2
Reputation: 704
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);
Upvotes: 1