Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

481
Views
I want to use innerHTML in javascript but it's not working

I need to pass an html line with an id coming from javascript, I used innerHTML but I can't get it to work, if I use a simple string it works

function passid(id){
   var lengthOfName = id
   document.getElementById('output').innerTEXT = "<input type="text" id="test" name="test" value="'lengthOfName'">";
    
};
<div id="output"></div>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you should use backticks and innerHTML.

    function passid(id){
      var lengthOfName = id
      document.getElementById('output').innerHTML = `
      <input type="text" id="test" name="test" value=${lengthOfName}>`;
   };
about 4 years ago · Juan Pablo Isaza Report

0

What you have to do is some small fixes

function passid(id){
   var lengthOfName = id
   document.getElementById('output').innerHTML = `<input type="text" id="test" name="test" value="${lengthOfName}">`;
    
};
about 4 years ago · Juan Pablo Isaza Report

0

In your example code, you are using innerTEXT with a string of HTML. This will not parse the HTML in your string and input element to the DOM.

Depending on how your are 'passing your id using javascript', you may need to defer the code as suggested in one of the comments. But you can use insertAdjacentHTML with 'afterbegin' as the placement of the HTML string. Also I would use template literals and encase your variable dollar sign and curly brackets like so => ${id}.

See my example, if this is not what you are looking for please let me know and I will properly address my answer.

let id = "myid";

function passid(id){
   let el = document.getElementById('output');
   let str = `<input type="text" id="test" name="test" value="${id}">`;  
   el.insertAdjacentHTML('afterbegin', str);
};

passid(id);
<div id="output"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!