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

226
Visualizações
How to dynamically change the css style in javascript

I need to dynamically change the css padding. So every tenth number, I want to increase the padding. Here is my code

    var padding = 200
    Object.keys(MassAddressDict).forEach(function(key) {
        console.log(key + " " + MassAddressDict[key]);

        var recieverData = MassAddressDict[key]

        var senderInfo = combineSenderInfo();
        var receiverInfo = combineReceiverInfo(recieverData);

        var index = Object.keys(MassAddressDict).indexOf(key);
        

        if (index % 10 === 0){

            padding = padding + 20
            console.log("Padding increased to ", padding)
            
        } 
        printWindow.document.write('<div style="display:flex;justify-content:space-between">');

        printWindow.document.write(senderInfo);
         printWindow.document.write('</div>');
        printWindow.document.write(receiverInfo);
        printWindow.document.write("<p style='padding-bottom: 200px'></p>")
    });

what I want to do is at every tenth item in the dictionary, increase the padding by say 20px. How can I implement this?

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

0

Try this, I think this will solve your problem

    
    var padding = 200;
    Object.keys(MassAddressDict).forEach(function(key) {
        console.log(key + " " + MassAddressDict[key]);

        var recieverData = MassAddressDict[key]

        var senderInfo = combineSenderInfo();
        var receiverInfo = combineReceiverInfo(recieverData);

        var index = Object.keys(MassAddressDict).indexOf(key);

        if (index % 10 === 0){

            padding = padding + 20
            console.log("Padding increased to ", padding)
            
        } 
        printWindow.document.write('<div style="display:flex;justify-content:space-between">');

        printWindow.document.write(senderInfo);
         printWindow.document.write('</div>');
        printWindow.document.write(receiverInfo);
        printWindow.document.write(`<p style='padding-bottom: ${padding}px'></p>`)
    });

about 4 years ago · Juan Pablo Isaza Relatório

0

First you'll need to get your padding out of your forEach, now it's resetting to 200 for each new object.

Then simply add your padding in your style (you can easily add a variable in a string in javascript using backticks and putting your var inside ${} `This is your var => ${myVar}`).

Your code should look like that :

var padding = 200
Object.keys(MassAddressDict).forEach(function(key, index) {
    console.log(key + " " + MassAddressDict[key]);

    var recieverData = MassAddressDict[key]

    var senderInfo = combineSenderInfo();
    var receiverInfo = combineReceiverInfo(recieverData);

    if (index % 10 === 0){
        padding = padding + 20
        console.log("Padding increased to ", padding)
    } 
    printWindow.document.write('<div style="display:flex;justify-content:space-between">');

    printWindow.document.write(senderInfo);
    printWindow.document.write('</div>');
    printWindow.document.write(receiverInfo);
    printWindow.document.write(`<p style='padding-bottom: ${padding}px'></p>`)
});

about 4 years ago · Juan Pablo Isaza Relatório

0

here is some way of the dynamic CSS style

  1 var div = document.getElementById('div1');
    
    // Clear Value
    div.setAttribute('style','');
      // OR
    div.removeAttribute('style');
    
    // Set Style / Append Style
  2  div.style.backgroundColor = '#ff0000';

  3  document.getElementById('#div1').style += "padding-top:40px";

  4  $("#div1").css("property",'value');

  5 Var Value=25;
     <div id='div1' style='padding: ${Value}px'></div>

you should read this for complete reference https://www.w3.org/wiki/Dynamic_style_-_manipulating_CSS_with_JavaScript

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