Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 div as you want.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading