0

I was doing some javascript while I noticed i can't replace a "<" word inside an html tag such as

<label id="label" > <eraseme> </label>
document.getElementById('label').innerHTML = document.getElementById('label').innerHTML.replace("<eraseme>", "Replaced");

JSFiddle

Any solution ?

6
  • 1
    voila: jsfiddle.net/bhqvbd3y/3 Commented Aug 13, 2014 at 16:49
  • works for me in my fiddle: jsfiddle.net/zsv1yzyo Commented Aug 13, 2014 at 16:49
  • 6
    Usually the browser escapes < in invalid HTML automatically. Have you tried &lt;? Commented Aug 13, 2014 at 16:50
  • the trick the browser pulled was convincing you the < never existed Commented Aug 13, 2014 at 16:51
  • 1
    Your mistake was to assume what .innerHTML contains, instead of actually looking at what it contains. Use console.log( document.getElementById('label').innerHTML);. Commented Aug 13, 2014 at 16:52

3 Answers 3

1

I was able to get it working by using the following in the replace function.

document.getElementById('label').innerHTML = document.getElementById('label').innerHTML.replace("&lt;", "Replaced");
Sign up to request clarification or add additional context in comments.

Comments

0

Replace < with an html number

http://jsbin.com/vehimu/1/edit

2 Comments

html entity, there is no such thing a html number :)
sorry i meant an html name ascii.cl/htmlcodes.htm ccording to this website
0

Use HTML characters "&lt;".

1 Comment

That's called entity and you will also need &gt;.

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.