0

In one function of my site, the inner HTML of a div element is changed to include a right arrow. This symbol is included in the innerHTML of "myDiv" as ➞

In a separate function, I am trying to match the innerHTML of "myDiv" with ➞

function myFunction() {
//Many lines of code then
    var myText = document.getElementById("myDiv").innerHTML;
    alert(myText);
    if (myText.match(/➞/g)) { //also tried with /➞/g
         alert('Yep');
         myText = myText.replace(/➞/g, " to yield ");
    }
//Many more lines
}

When I alert myText as in the function above (before the if statement), the alerted output contains an actual right arrow ➞, not ➞ The problem is that even though ➞ is present in the code to produce the innerHTML of "myDiv" in the first place (in a separate function), in myFunction() ➞ does not seem to match, since the alert('Yep') is not called.

I looked up and in desperation tried decodeURIComponent() but I was pretty sure this was not going to work and it seemed not to. Can anyone help with this problem? Thanks

1 Answer 1

1

Check it against an actual right arrow, then.

function myFunction() {
//Many lines of code then
    var myText = document.getElementById("myDiv").innerHTML;
    alert(myText);
    if (myText.match(/➞/g)) { //also tried with /➞/g
         alert('Yep');
         myText = myText.replace(/➞/g, " to yield ");
    }
//Many more lines
}
Sign up to request clarification or add additional context in comments.

3 Comments

Nice...this worked perfectly. Thank you. So I suppose the lesson here is the innerHTML of any element which includes a non-keyboard character actually contains the non-keyboard character, not the HTML code for that character?
@TheOneandOnlyChemistryBlob: It’s not a non-keyboard character; it’s any character. But yes, unless it’s necessary for escaping. (You should try to avoid innerHTML and work with actual text when you can; it doesn’t have that kind of inconsistency.)
This solution worked but I just tried matching the different right arrow represented by → (there are multiple different directional arrows) and this matched too. I guess an arrow is an arrow is an arrow as far as matching innerHTML is concerned :)

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.