As someone who is new to JavaScript, I am currently exploring the fundamentals of this programming language. To practice, I have attempted to create a simple text editor for a webpage. Specifically, I am working on a feature that can identify whether the word "bob" has been entered into a textarea using JavaScript.
<script>
function findBob() {
var inputText = document.getElementById("box").innerHTML;
if(inputText === "bob") {
document.getElementById("box").innerHTML = "true";
}
if (inputText !== "bob") {
document.getElementById("box").innerHTML = "false";
}
}
</script>
<textarea rows=10; col= 5; id="box">
bob</textarea>
<button onClick="findBob();">FIND BOB</button>
When I click the "FIND BOB" button, it correctly changes the text to "true" if the word "bob" is present in the textarea. However, if I add random characters to the textarea and then click "FIND BOB" again, nothing happens. The function only seems to work when the textarea contains exactly the word "bob". Even after reloading the page or editing the textarea content, the function still does not execute properly.
I suspect that there may be an error in my JavaScript syntax, but I cannot pinpoint it. What could be the issue?
-EDIT- I decided to use ===
for comparison due to a recommendation from another discussion on StackOverflow, which you can find here.