2

I use × HTML entity. I want to replace × by * using JS.

Ad far I tried...

val = val.replace('×','*');

and

val = val.replace(/×/g,'*');

Output Come: 4 × 5 But, expected output should be 4 * 5 .

<html>
  <p id="entry">4 &times; 5</p>
  <script>
      var val;
      val = document.getElementById("entry").innerHTML;
  </script>
</html>

How to do it? Can you say what was conceptually fault I did?

1 Answer 1

4

Only entities that represent special characters like <, > and & are returned as their entity text from innerHTML.

In this case, &times; doesn't represent a special character and it is converted to ×, to replace it, you can use .replace('×', '*').

Here is an example:

entry.innerHTML = entry.innerHTML.replace('×', '*');
<p id="entry">4 &times; 5</p>

And here is a snippet illustrating how different entities are represented by innerHTML:

console.log(entry.innerHTML);
<p id="entry">&amp; &lt; &gt; &times; &#36; &#37; &#39;</p>

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

1 Comment

Yes, It is work and I also understand my problem. Thanks

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.