javascript - html tag inside innerHTML -
html
<a href="#1" class="current">1</a> <div id="xyz"></div> javascript
function updatehash() { var number = document.queryselectorall('.current')[0].innerhtml; document.queryselector('#xyz').innerhtml = '<a>'+ number + '</a>'; } updatehash(); obviously, works.
however, need <a> <a href="#>
so when that:
document.queryselector('#xyz').innerhtml = '<a href="#'+ number + '"</a>'; it fails.
i don't know if can resolved escaping character; when tried escape " & #, didn't work.
maybe there isn't way escape character scenario.
any appreciated.
[update]
i solved way.
function updatehash() { var number = document.queryselectorall('.current')[0].innerhtml; var tag = '<a href=#' document.queryselector('#xyz').innerhtml = tag + number + '>next</a>'; } updatehash(); actually, doing same thing before. pointed out, wasn't seeing output because href empty. lol
- you not closing
atag<a></a> - and
innerhtmlofatag empty .so not visible there inbody
your mistake .kindly check outerhtml
function updatehash(){ var number = document.queryselectorall('.current')[0].innerhtml; document.queryselector('#xyz').innerhtml = '<a href="#'+ number + '"</a>'; console.log(document.queryselector('#xyz').outerhtml) } updatehash(); <a href="#1" class="current">1</a> <div id="xyz"></div> solved
function updatehash(){ var number = document.queryselectorall('.current')[0].innerhtml; document.queryselector('#xyz').innerhtml = '<a href="#'+ number + '">innerhtml empty</a>'; console.log(document.queryselector('#xyz').outerhtml) } updatehash(); <a href="#1" class="current">1</a> <div id="xyz"></div>
Comments
Post a Comment