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

  1. you not closing a tag <a></a>
  2. and innerhtml of a tag empty .so not visible there in body

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

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -