1

I have a paragraph of text and want a user to be able to type in the numbers for where a substring should start and end, then have that substring of text printed out when they click a button. But nothing happens when the button is clicked.

Google Chrome gives me this error on the console:

Uncaught TypeError: Cannot read property 'substring' of undefined

HTML

<div>
    <h3 id="sentence">You can input numbers below and it will return that interval of characters from this sentence.</h3>
    <input type="text" id="input1">
    <input type="text" id="input2">
    <button type="button" onclick="interval()">Submit</button>
    <p id="intervalAnswer"></p>
</div>

JavaScript:

function interval() { 
var inputStart = document.getElementById("input1").value
var inputEnd = document.getElementById("input2").value
var string = document.getElementById("sentence").value;
var result  = string.substring(inputStart, inputEnd);
document.getElementById("intervalAnswer").innerHTML = result;
}
1
  • 1
    For <h3> use innerHTML instead of value. Commented Nov 22, 2015 at 1:31

1 Answer 1

1
var string = document.getElementById("sentence").textContent;

This was your problem, h3 does not have value.

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

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.