I'm trying to replace a part of my HTML code with JS but doesn't work.
I have the next code:
<td id="one"><a href="servlet.jsp?account='old'" >old</a></td>
And i'm want to change "old" for "new", like this:
<td id="one"><a href="servlet.jsp?account='new'" >new</a></td>
Then i'm using JS because i need to do with this using the method replace(), then, my JS is:
var old = "old";
var rep = "new";
var elem = document.getElementById("one");
var text = elem.innerHTML;
text = text.replace(old,rep);
elem.innerHTML = text;
I tryed and search but always i founded the same.
Any suggestion are welcome.
(I wrote the cody by memory because i'm not in the office and it's more complex) i'll try tomorrow)
2 Answers 2
Because "new" is a reserved word in JavaScript.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words
1 Comment
No clue - it works for me. However, your method is very insecure; instead of working on elem.innerHTML, it would be much better to use elem.getAttribute and elem.setAttribute for direct access to href. Otherwise, imagine what would happen if your old happens to be "ref", or something similar that coincidentally exists before your intended term...
EDIT: doh, didn't even notice the var name>.<
SyntaxError: Unexpected token new. Learn how to debug JavaScript.elem, get the child<a>element, and usereplaceon itshrefproperty instead. You shouldn't have to reset theinnerHTMLof its parent