![]() |
![]() |
CharliePrince |
![]()
Post
#1
|
Novice ![]() ![]() Group: Members Posts: 24 Joined: 7-November 20 From: Saint Louis, MO Member No.: 27,623 ![]() |
I'm trying to replace an html symbol code '÷' with another string.
I can't get this to work. See example below. CODE <body> <p id='target'>A ÷ B</p> <script> console.log(`${document.getElementById('target').innerText}`); console.log(`${document.getElementById('target').innerText.replaceAll('÷', '/')} (want this to = 'A / B') `); console.log(`${document.getElementById('target').innerText.replaceAll('÷', '/')} (or, want this to = 'A / B') `); </script> </body> Can anyone help? Pandy? |
![]() ![]() |
CharliePrince |
![]()
Post
#2
|
Novice ![]() ![]() Group: Members Posts: 24 Joined: 7-November 20 From: Saint Louis, MO Member No.: 27,623 ![]() |
I didn't notice your most recent posts here until after my last reply above.
Yes - it's odd to me why some of these are interpreted or rendered literally or as their encoded char or whatever. Crap! I don't know the words to describe but I think you and Christian know what I mean ![]() Sorry about the console.logs but . . Look at this (below) . . . if it's of any interest where currentString is broken down to each character CODE console.log(currentString); for (i = 0; i < currentString.length; i++) { console.log(currentString[i] + ' = ' + currentString.charCodeAt(i)); } |
Christian J |
![]()
Post
#3
|
. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: WDG Moderators Posts: 9,687 Joined: 10-August 06 Member No.: 7 ![]() |
I don't know the words to describe but I think you and Christian know what I mean ![]() We sure do. I usually reserve this smilie for situations like this: ![]() QUOTE Sorry about the console.logs but . . Look at this (below) . . . if it's of any interest where currentString is broken down to each character CODE console.log(currentString); for (i = 0; i < currentString.length; i++) { console.log(currentString[i] + ' = ' + currentString.charCodeAt(i)); } I don't understand, will it not just return the charCode for each character in currentString? Except if you feed it CODE & of course, then it will return CODE & & = 38 a = 97 m = 109 p = 112 ; = 59 ![]() |
pandy |
![]()
Post
#4
|
🌟Computer says no🌟 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: WDG Moderators Posts: 20,744 Joined: 9-August 06 Member No.: 6 ![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() HTML <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('\367', '/'); document.getElementById('target').innerHTML = replacestring; } If the entity is one of the not 'interpreted' ones it doesn't work right off, because there javascript seems to see the HTML entity as a string of characters. For ampersand I had to do this or only the literal ampersand would be replaced, not the whole entity. I find this very confusing, because if it is a sting, why can't it be replaced the normal way? HTML <p id='target'>A & B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var entity = '\46' + 'amp;'; var replacestring = currentstring.replace(entity, 'and'); document.getElementById('target').innerHTML = replacestring; } So it gets hairy anyway. Not for a unique case like with divide, but if you don't know what entities there may be. There could be more 'not interpreted' ones than ones we've found. There must be some documentation for this! Maybe one could check for & and ; and use different methods accordingly, but those characters could occur otherwise in the text too, so I don't really see how this could work unless a list of not interpreted entities is used... |
Christian J |
![]()
Post
#5
|
. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: WDG Moderators Posts: 9,687 Joined: 10-August 06 Member No.: 7 ![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() HTML <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> CODE function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('\367', '/'); document.getElementById('target').innerHTML = replacestring; } Strange, this does not work: CODE <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> <script type="text/javascript"> function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('÷', '/'); // Note that I don't use an entity here. document.getElementById('target').innerHTML = replacestring; } </script> --why isn't the entity "interpreted" now? Is the replace() method different? ![]() |
pandy |
![]()
Post
#6
|
🌟Computer says no🌟 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: WDG Moderators Posts: 20,744 Joined: 9-August 06 Member No.: 6 ![]() |
Strange, this does not work: CODE <p id='target'>A ÷ B</p> <button onclick="doIt()">Click!</button> <script type="text/javascript"> function doIt() { var currentstring = document.getElementById('target').innerHTML; var replacestring = currentstring.replace('÷', '/'); // Note that I don't use an entity here. document.getElementById('target').innerHTML = replacestring; } </script> We already knew that. CODE --why isn't the entity "interpreted" now? Is the replace() method different?:unsure: And that too. It is interpreted, but it doesn't work to use the actual character for the replace. Those too things are what was so darn odd! I was googling html entities and javascript and stumbled on this page. Then I got the idea that maybe if one used the character code JS prefers... And it worked. ![]() https://brajeshwar.github.io/entities/ |
Christian J |
![]()
Post
#7
|
. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: WDG Moderators Posts: 9,687 Joined: 10-August 06 Member No.: 7 ![]() |
I was googling html entities and javascript and stumbled on this page. Then I got the idea that maybe if one used the character code JS prefers... And it worked. ![]() https://brajeshwar.github.io/entities/ Seems I already had that page bookmarked, we must have been discussing this before. |
pandy |
![]()
Post
#8
|
🌟Computer says no🌟 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: WDG Moderators Posts: 20,744 Joined: 9-August 06 Member No.: 6 ![]() |
Nope. I didn't know about this and I hadn't seen that page. Well, I knew that kind of octal codes could be used in JS, but I didn't know they needed to be used in replace strings.
I may add that I still don't understand why they need to be used... ![]() In a case like this the ampersand doesn't need to be escaped. What's the difference? CODE var test = 'this & that'; alert(test); Are there other cases when these characters need escaping? |
![]() ![]() |
![]() |
Lo-Fi Version | Time is now: 23rd June 2024 - 03:14 AM |