Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda