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

202
Views
Creating a div and inserting HTML

I'm trying to create this in Javascript so that I can insert it into the document:

    <div class="listing-small-badge award-badge" style=" vertical-align: top; position: relative; top: -250px; z-index: 2;"><i class="fa fa-tag award" style="background-color: #990000;"></i>Highest Rated</div>

And I've tried everything I can think of, but once I create the 'i' element, I can't seem to properly append it to the div element.

For example:

    <script type="text/javascript">
    var div = document.createElement("div");
    div.style.cssText += 'vertical-align:top; position: relative; top:-250px; z-index:2;';
    div.classList.add("listing-small-badge");
    div.classList.add("award-badge");
    div.innerHTML = "Highest Rated"; //this works on its own, but disappears if/when I try to append the 'i' element/HTML tag
    var i = document.createElement("i");
    i.classList.add("fa");
    i.classList.add("fa-tag");
    i.classList.add("award");
    //i.style.cssText("background-color: #990000;"); //doesn't like this for some reason TBD
    //console.log(i); //prints out just fine
    //div.innerHTML += i; //results in an empty div
    //div.innerHTML += '<i class="test">x</i>'; //results in an empty div
    //div.insertAdjacentHTML("afterend", "<i class='test'></i>"); //results in "The element has no parent."
    console.log(div);
    </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Here is how you can dynamically add html using js

//js file

function createElements() {
  var div = document.createElement("div");

  div.classList.add("listing-small-badge");
  div.classList.add("award-badge");
  div.innerHTML = "Highest Rated";
  div.innerText='Dusting';
  var i = document.createElement("i");
  i.classList.add("fa");
  i.classList.add("fa-tag");
  i.classList.add("award");
  i.href = 'google.com';
  i.innerText = 'google';
  div.appendChild(i);
  const body = document.querySelector('body');

  body.appendChild(div);


    window.onload = (e)=>{

      createElements();

    }
    
    }

//html

<body>
  
</body>
about 4 years ago · Juan Pablo Isaza Report

0

I'm at a loss as to why jQuery would be necessary, but here's what worked:

    jQuery(document).ready(function($){
        var htmlData = '<div class="listing-small-badge award-badge" style="vertical-align: top; position: relative; top: -250px; z-index: 2;"><i 
        class="fa fa-tag award" style="background-color: #990000;"></i>Highest Rated</div>';
        $('body p').append(htmlData);
    });
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!