In textarea
, I want to determine the position of the last "†" character.
var textField = document.getElementById("main_field"); console.log(textField.value.indexOf("†"));
<textarea id="main_field">Text † entered by user † †</textarea>
While this works fine here, I get weird results in the browser
However, it cannot find the character and returns -1.
I try to understand the inner performance of the character. It outputs as "-" in the console, which I find strange.
First, let's see how it works.
Now that we've seen this, we need to find out what's wrong with your code. First, you need to check if
textField.value
contains the characters you are searching for. Next, look at this call in the code:This actually gets the substring starting from the beginning up to
startPos
. Judging from the name ofstartPos
, it seems that instead of getting the string from beforestartPos
, it gets it fromstartPos
.