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

125
Visualizações
Select element inside jquery

I was making a vanilla javascript bar graph where I made the bar inside the li tag. In the code below, i use the attribute within the li tag to set the height of the bar, but when I try to select the bar from within the li tag. I am unable to do so.


<ul class="GraphWrapper">
                        <li  bar-height="100">
                            <div class="graph-bar bar"></div>
                            MON
                        </li>
                        <li  bar-height="190">
                            <div class="graph-bar bar"></div>
                            TUES
                        </li>
                        <li  bar-height="260">
                            <div class="graph-bar bar"></div>
                            WED
                        </li>
                        <li  bar-height="180">
                            <div class="graph-bar bar bar-active"></div>
                            THURS
                        </li>
                        <li  bar-height="220">
                            <div class="graph-bar bar"></div>
                            FRI
                        </li>
                        <li  bar-height="300">
                            <div class="graph-bar bar"></div>
                            SAT
                        </li>
                        <li bar-height="50">
                            <div class="graph-bar bar"></div>
                            SUN
                        </li>
                    </ul>


//JAVASCRIPT


var children=$('.GraphWrapper').children();
for(var i=0;i<children.length;i++){

    let hgt = children[i].getAttribute('bar-height');

    bar = children[i] > $('.bar');

    bar.css('height',hgt+'px');
}

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

0

Here is what you need:

var children = $('.GraphWrapper').children();
for (var i = 0; i < children.length; i++) {
    let kid=$(children[i]);
    let hgt=kid.attr('bar-height');
    let bar=kid.find(".bar");
    bar.css('height', hgt + 'px');
}

Because the children() function return a list of DOM object, so you need to convert the children[i] to jQuery object, and then use the find method to find all divs in the li element, finally, change the height of the div as you want.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .each() method in jQuery and .forEach() method in JavaScript. I putted JavaScript snippet for this also. Check blow snippet I

Pure JavaScript Code:

[...document.querySelectorAll('.GraphWrapper li')].forEach((el)=>{
   el.firstElementChild.style.height =  el.getAttribute('bar-height')+'px' 
});

hope this will help you a lot.

Helpful Links:
.each() Method
https://api.jquery.com/each/

.forEach() Method
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

$('.GraphWrapper li').each((i,elem)=>{
    let ht = $(elem).attr('bar-height');
    $(elem).find('.graph-bar').css('height', ht+'px')
});
.GraphWrapper{
    width: 100%;
    max-width: 360px;
    border: 1px solid #eee;
    display: flex;
    justify-content: center;
    align-items: baseline;
    list-style: none;
    padding: 10px 5px;
}
.GraphWrapper li{
    width: 40px;
    font-size: 12px;
    text-align: center;
    margin-left: 5px;
    margin-right: 5px;
}
.graph-bar{
    background-color: #ccc;
    width: 100%;
    height: 0;
    margin-bottom: 5px;
    transition: 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="GraphWrapper">
    <li bar-height="100">
        <div class="graph-bar bar"></div>
        MON
    </li>
    <li bar-height="190">
        <div class="graph-bar bar"></div>
        TUES
    </li>
    <li bar-height="260">
        <div class="graph-bar bar"></div>
        WED
    </li>
    <li bar-height="180">
        <div class="graph-bar bar bar-active"></div>
        THURS
    </li>
    <li bar-height="220">
        <div class="graph-bar bar"></div>
        FRI
    </li>
    <li bar-height="300">
        <div class="graph-bar bar"></div>
        SAT
    </li>
    <li bar-height="50">
        <div class="graph-bar bar"></div>
        SUN
    </li>
</ul>

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