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.
P粉0434322102023-09-08 09:31:02
First, let's see how it works.
console.log(document.getElementById("textField1").value.lastIndexOf("†"));
console.log(document.getElementById("textField2").value.lastIndexOf("†"));
<textarea id="textField1" value="a†b†c†d"></textarea>
<textarea id="textField2">a†b†c†d</textarea>
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:
.substring(0, startPos)
This actually gets the substring starting from the beginning up to startPos
. Judging from the name of startPos
, it seems that instead of getting the string from before startPos
, it gets it from startPos
.