Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
How do I get text to display below an anchor?

I'm trying to make a button that displays your computer's current battery level. The way it works is, you click a button and in the JavaScript console, it displays your computers current battery level. Instead of putting the level in the console, is there a way to display the level below the anchor tag?

function batlevel() {
  navigator.getBattery().then(function(battery) {

    var level = battery.level;

    console.log(level);
  })
};
<a onclick="batlevel()">hi</a>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Pretty straight forward. Get the element you want to append to, create a tag, set innerText and then append

function batlevel() {
  navigator.getBattery().then(function(battery) {

    var level = battery.level;
    var p = document.createElement("p");
    
    p.innerText = level;
    
    document.getElementsByTagName('a')[0].append(p)

    console.log(level);
  })
};
<a onclick="batlevel()">hi</a>

about 4 years ago · Juan Pablo Isaza Relatório

0

function batlevel() {
  navigator.getBattery().then(function(battery) {

    var level = battery.level;

    console.log(level);
    document.getElementById('levelId').innerHTML = 'this is the level' + level;
  })
};

<a onclick="batlevel()">hi</a>
<div id="levelId"></div>
about 4 years ago · Juan Pablo Isaza Relatório

0

Consider the following.

function getBatLevel(target) {
  var level;
  navigator.getBattery().then(function(battery) {
    level = (battery.level * 100);
    if (target == undefined) {
      console.log(level + "%");
    } else {
      target.after(" " + level + "%");
    }
  });
};
<a onclick="getBatLevel(this)">Get Level</a>

This makes use of the .after() call: https://developer.mozilla.org/en-US/docs/Web/API/Element/after

You can wrap it in a Element if you choose.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda