0

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');
}

4
  • 1
    children[i] > $('.bar'); returns boolean Commented Jun 2, 2022 at 3:23
  • What should I do instead? Commented Jun 2, 2022 at 3:26
  • I don't undestand what do you want to do exactly. Could you describe it a little bit more? Commented Jun 2, 2022 at 3:27
  • I want to select the bar class so that I could change it's height according to the iteration(i) it is inside of. for Example; if the:- i=2 then i want to get the bar class inside the i of that particular instance. Commented Jun 2, 2022 at 3:29

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.