How to create an Accordion using CSS & Javascript

Sdílet
Vložit
  • čas přidán 12. 09. 2024

Komentáře • 147

  • @lernist.oficial
    @lernist.oficial Před 3 lety +13

    I have been watching tutorials for a week, now, at 3am, your video has finally made me finished the final step of my project, thanks a lot man, keep it up

  • @sfrstatus9070
    @sfrstatus9070 Před 4 lety +14

    Guys an easier method:
    Heading
    Lorem
    Then style it

    • @arkadio638
      @arkadio638 Před 3 lety

      wow so simple :O

    • @sulaimandev
      @sulaimandev Před 3 lety

      Then how will you learn Javascript?

    • @versatilegeniuses9374
      @versatilegeniuses9374 Před 3 lety +1

      @@sulaimandev the people who are making projects should learn Javascript first and then come here

    • @HongKong1842
      @HongKong1842 Před 3 lety

      Do you mean it takes fewer DIV ?

    • @wisdomelue
      @wisdomelue Před 3 lety

      how do you add transitions to this?

  • @anasyoussi514
    @anasyoussi514 Před 3 lety +7

    Using ForEach :
    const accordion = document.querySelectorAll('.contentBx') ;
    accordion.forEach(acc => {
    acc.addEventListener('click', function(){
    this.classList.toggle('active');
    })
    })

  • @kieran2347
    @kieran2347 Před 4 lety +5

    This is amazing. thanks for this video especially for keeping them short and to the point and ofcourse covering all the parts

  • @rayjenscode3501
    @rayjenscode3501 Před 3 lety +4

    nice tutorial. Did you try height 100%? It seems the animation won't effect.

  • @opeyemiodebowale1845
    @opeyemiodebowale1845 Před 2 lety +2

    Thank you so much for this. Please is there a way to remove the scroll bar, because mine is showing like a dot even when the tab is closed, so the user is able to scroll when the tab is closed from that dot scroll bar... is there a way to remove the scroll bar entirely?

  • @yvesyao3529
    @yvesyao3529 Před 4 lety +4

    You are reading my mind teacher, I was trying to do that too. too loud, thank you teacher

  • @RIGARR
    @RIGARR Před 3 lety +1

    thanks bro... I got sucked up here from the last hour and helped me..

  • @eder.santana_oficial
    @eder.santana_oficial Před 4 lety +9

    Eu já entro dando like, seus vídeos são bons de mais

    • @stomperhk9107
      @stomperhk9107 Před 3 lety

      KKKK, aposto que ele deve tá entendendo tudo

  • @shahadomar4120
    @shahadomar4120 Před 3 lety +1

    Wow this was so easy and clear , I didn't expect that 😂

  • @frankfarfan3107
    @frankfarfan3107 Před 2 lety

    Thank yout so much, I made the code in another way but I used yout logic and now my web project is neer to be finished :D, muchas gracias hermano.

  • @tadakuniyasuda8214
    @tadakuniyasuda8214 Před 4 lety

    AMAZING!!!!!!!!!!!!!!!!!!!!!!!!!! Thank you . you are my hero.

  • @roger6388
    @roger6388 Před 4 lety

    Wow you are the best I have learned many styles of css because of you.Thanks alot 😃 😊

  • @NejdetACAR
    @NejdetACAR Před 4 lety +11

    I think there's a logic error (in my opinion) in the code running
    After the content box is opened, it should only close when the content title is clicked, but it also closes when the content text is clicked.
    In other words, I want the opening and closing operations to be done only when the content title is clicked. I think this is what makes sense.
    How can I fix this problem.

    • @HongKong1842
      @HongKong1842 Před 3 lety +1

      It actually becomes convenient when a reader clicks a larger area to close it. No need to be precise. Do you agree?

    • @beaq6755
      @beaq6755 Před 2 lety +3

      @@HongKong1842 no, because I for example need the user to use inputs and buttons in the content part. How are they gonna use anything if as soon as they click the content disappears?

    • @brangja4815
      @brangja4815 Před 2 lety +1

      You can achieve that like this.
      - Give the text tag a class name.
      - in eventListener function
      function(e) {
      if(e.target.closest("class_name_of_the_text_tag")) return;
      this.classList.toggle("active");
      };

    • @kokonut5498
      @kokonut5498 Před rokem

      @@brangja4815 can you elaborate on this I tried what you said but it still doesnt work.

    • @donatocontinanza4331
      @donatocontinanza4331 Před rokem

      @@brangja4815 there is a better and easier solution.. by my opinion:
      const accordion = document.getElementsByClassName("content-box");
      for(let i = 0; i < accordion.length; i++){
      accordion[i].firstElementChild.addEventListener("click", function() {
      accordion[i].classList.toggle("active");
      })
      }
      the firstElementChild will get the element ();
      obviously if you wounld insert other child elements first, you should change this property with children[nth]
      ...accordion[i].children[2].addEventListener("click", function() ....
      @kokonut5498

  • @karimaziz8857
    @karimaziz8857 Před rokem

    Great tutorial man, that really helped

  • @codewithegai
    @codewithegai Před 2 lety

    Very concise. Thanks so much this was really helpful.

  • @codecleric4972
    @codecleric4972 Před 3 lety

    Great tutorial, to the point and it works...thank you..

  • @raguinfotech6639
    @raguinfotech6639 Před 2 lety

    super very use full title thank you

  • @Angryjenkinsdigital
    @Angryjenkinsdigital Před 3 lety +1

    getting bugs on this with iOS/safari - namely issues toggling the active class. Are there known fixes for iOS/Safari?

  • @chandrakanthpadi
    @chandrakanthpadi Před 4 lety

    You are doing an awesome work.

  • @newcode7847
    @newcode7847 Před 2 lety

    Thank you, great and simple tutorial.

  • @emcasaempreendimentosimobi3229

    thanks, i was in need of that.
    Gratitude

  • @maxlong1374
    @maxlong1374 Před 3 lety

    Coding & Piano, best match of all!

  • @AnjuGupta-jy6lj
    @AnjuGupta-jy6lj Před 3 lety

    Which text editor you are using ?

  • @Dreamingofrainbows
    @Dreamingofrainbows Před rokem +1

    hey did anyone have any issues with their code? My toggle class doesn't work, everything seems to be fine with the code, but toggle isn't enabled. Any recomms?

  • @ziledjordjevic695
    @ziledjordjevic695 Před 3 lety

    Thank you for this tutorial.

  • @vietnguyenZ2
    @vietnguyenZ2 Před 4 lety +2

    It's nice but not dinamic. Because you fixed height when collapse. Scroll appear when content so long. I want more...

    • @OnlineTutorialsYT
      @OnlineTutorialsYT  Před 4 lety +1

      Add Overflow-Y: auto; in .contntBx

    • @vietnguyenZ2
      @vietnguyenZ2 Před 4 lety +1

      ​@@OnlineTutorialsYT I agree about scroll. But more case, content maybe short or long height, fixed height 150px is not perfect. Not same bootstrap collapse with all case

  • @boyemarc-antoine7027
    @boyemarc-antoine7027 Před 2 lety +1

    if u dont put a fixed haight value, animation dont work.

  • @webdevwiz5636
    @webdevwiz5636 Před 3 lety

    Amazing work! Thanks for sharing.

  • @prasaddhend5500
    @prasaddhend5500 Před 4 lety +1

    Brother when we click on one box another box *automatically* closed.
    Like - when we click on 1st box
    2,3,4 boxes close *automatically*
    When we click on 3rd box
    1,2,4 boxes *automatically*.

  • @ahangaraadil
    @ahangaraadil Před 4 lety

    That is totally sick. 👨‍💻

  • @Mieystery
    @Mieystery Před 9 měsíci

    Can i make it already opened?

  • @soullessleftover8427
    @soullessleftover8427 Před 4 lety

    Say any chance of making a grid like Xiaomi's website for products? Looks really minimalistic and is very polished.

  • @anaghakamble1293
    @anaghakamble1293 Před 2 lety

    Amazing video! very helpful, Thanks

  • @seenuiropias553
    @seenuiropias553 Před 4 lety

    Thank you. Very useful.

  • @GuilhermeSilva-ex1qk
    @GuilhermeSilva-ex1qk Před 4 lety

    AWESOMEEEEE! VERY VERY GOOD BRO!

  • @shassan7
    @shassan7 Před 11 měsíci

    height of conent should be changable due to text

  • @mdsumonbbk8125
    @mdsumonbbk8125 Před 3 lety

    This is very helpfull for me.

  • @ericdimarzio5756
    @ericdimarzio5756 Před rokem +1

    A few others have mentioned this. I lose the transition animation when I try to set the active height to 100%, fit-content, or auto. Any solutions? It would be more responsive if the height was dynamic.

    • @janemig
      @janemig Před 22 dny

      Was there an answer to this? I am having the same issue.

    • @ericdimarzio5756
      @ericdimarzio5756 Před 22 dny

      @@janemig Kevin Powell has some new videos on recently added css animations for display none and display auto. Check his channel out.

  • @mr.guardian4491
    @mr.guardian4491 Před 4 lety +1

    Sir, how you learn these advanced things? Please tell me

  • @Yogibaba_youtubewala
    @Yogibaba_youtubewala Před 4 lety

    Awesome as usual

  • @AnujGupta-kw9sw
    @AnujGupta-kw9sw Před 4 lety

    Easiest than I've ever seen

  • @soft-coding
    @soft-coding Před 4 lety +1

    Thanks!!

  • @mhassankhan8677
    @mhassankhan8677 Před 3 lety

    what's the use of position:relative in your code? It's everywhere.

  • @akview6460
    @akview6460 Před 4 lety

    can you make some different nav bar which seems classic and beautiful

  • @donny533
    @donny533 Před 4 lety +1

    awesome! is there a way to make the effects horizontal instead of vertical?

  • @dinushaanupama3646
    @dinushaanupama3646 Před rokem

    Good Video !

  • @CNUVAS0236
    @CNUVAS0236 Před 4 lety

    can u design thumbnail slider/carousel with icons(forward & backward) on top on either right or left?

  • @humaamwebdev6050
    @humaamwebdev6050 Před 2 lety +1

    mazaa agya

  • @doyelbhattacharjee3592

    Thank for making this🙏🙏

  • @snssatyabhagavan
    @snssatyabhagavan Před 4 lety +1

    I set alarm to see the video first but failed 😁
    Nice

  • @iyabosawa5128
    @iyabosawa5128 Před rokem

    Very nice Teacher

  • @cyrusgracias4556
    @cyrusgracias4556 Před 4 lety +4

    We can build accordion using HTML details and summary elements easily

    • @wisdomelue
      @wisdomelue Před 3 lety

      how do you add transitions to them, transitions seem not to take effect

  • @crazycoder9356
    @crazycoder9356 Před rokem

    You should be using event delegation.

  • @BWVidventures
    @BWVidventures Před 10 měsíci

    sir do u have a source code uploaded?

  • @htetwaiyan2499
    @htetwaiyan2499 Před 3 lety

    Very cool. Easy JS

  • @anandhakumar8898
    @anandhakumar8898 Před rokem

    I want to close the first row if I clicked the second row. How can I do that?

  • @liamaiken1782
    @liamaiken1782 Před rokem

    thanks

  • @IbrahimJalloh
    @IbrahimJalloh Před 3 lety

    Awesome, thanks...

  • @saidibrahim5931
    @saidibrahim5931 Před 3 lety +1

    Why toggle is not working for me, I did everything in this tutorial

  • @maskman4821
    @maskman4821 Před 4 lety +1

    This is vey useful, accordion seems play an important role in ui/ux, it is cool to have such an ui element in web page !!!

  • @phattrienbanthan360do

    Very nice!!!!

  • @surenderoati
    @surenderoati Před 4 lety

    just awesome 🥰🥰

  • @roshansajeev4855
    @roshansajeev4855 Před 4 lety

    Hi onlinetut do you have a discord channel

  • @paulohrl13
    @paulohrl13 Před 3 lety

    Very niceee man s2s2s2 thank you very much

  • @NorthernChimp
    @NorthernChimp Před rokem

    1:24 `body { display: flex; ` why?

  • @storytellersabanti
    @storytellersabanti Před 3 lety

    Can you create a tutorial -- when open a tab then automatically close the open tab

  • @hanutsai6860
    @hanutsai6860 Před 4 lety +1

    very good

  • @shubhamchandel7813
    @shubhamchandel7813 Před 3 lety

    can anyone please explain to me why the "active" class was added before the content. i didn't get the logic. I'm new so it would mean a lot if someone clears this doubt. i don't even know the name of the concept so I cant search the web for it.

  • @user-wx5td9tw2h
    @user-wx5td9tw2h Před 4 lety

    Hi! How make accordion in the accordion? Say me please.

  • @tanishbhamare3365
    @tanishbhamare3365 Před 4 lety

    Awesome!👍👍

  • @Nehayadav-nh5zy
    @Nehayadav-nh5zy Před 2 lety

    Hey I am unable to see the active tab

  • @chemedev
    @chemedev Před 4 lety

    Impecable!

  • @gopalsaini4704
    @gopalsaini4704 Před 3 lety

    thanks a lot sir

  • @amuchinonso9459
    @amuchinonso9459 Před rokem

    hey!!
    my CSS are not reflecting??😮‍💨😮‍💨 help plsssss

  • @AJKING-tq3pd
    @AJKING-tq3pd Před 4 lety

    Sir please show how to make a calculater with the input from keyboard. I really worked hard but I didn't successed.

  • @renukagargicollege1486
    @renukagargicollege1486 Před 4 lety +1

    nice video

  • @elementordesigner
    @elementordesigner Před rokem

    How to creat a rotate effect icon on ckick?

  • @ismoilxonkhamidullaev3080

    Nice

  • @felipedalbert1342
    @felipedalbert1342 Před rokem

    thanks for this

  • @Venistro
    @Venistro Před 4 lety +1

    Why with JavaScript and not CSS/HTML only?

    • @HongKong1842
      @HongKong1842 Před 3 lety

      Would it become easier or harder? This version is quite simple enough to me already.

    • @Venistro
      @Venistro Před 3 lety

      @@HongKong1842 I would say simpler. But I would prefer CSS only, cause it still works when someone disable/blocks Javascript. Sometimes maybe because a self created adblocker filter blocks to much and such things.

    • @HongKong1842
      @HongKong1842 Před 3 lety

      @@Venistro u mean js is simpler.

    • @Venistro
      @Venistro Před 3 lety

      @@HongKong1842 CSS only is simpler in my opinion. It uses only checkboxes and css selectors.

    • @HongKong1842
      @HongKong1842 Před 3 lety

      @@Venistro perhaps you write and share a version for us to compare.

  • @robertoacuna4667
    @robertoacuna4667 Před 2 lety

    This is okay but if you click the body content it will be closed automatically, nice for viewing information but not in transactional HTML users

  • @michael_jordan_g
    @michael_jordan_g Před 3 lety

    amazing

  • @kumarianshu-zl6jo
    @kumarianshu-zl6jo Před rokem

    heii my active is not working

  • @twitchplus3593
    @twitchplus3593 Před 4 lety

    Awesome

  • @kkolenka
    @kkolenka Před 2 lety

    thanks. cool

  • @lefamofokeng8291
    @lefamofokeng8291 Před 11 měsíci

    Source code??

  • @MuthuKumar-qx2ne
    @MuthuKumar-qx2ne Před 3 lety

    Super

  • @javaranjeet
    @javaranjeet Před rokem +1

    send me code 🖥🖥 , Please 🙏🙏

  • @wtfmomentninenine
    @wtfmomentninenine Před 4 lety

    Iam from Indonesia 🇮🇩

  • @peshotod
    @peshotod Před 4 lety

    somehow the script doesn't work

  • @ahmadbinabaskpm-guru6532

    SIR CAN SHARE THE CODING

  • @ultimategaming517
    @ultimategaming517 Před 4 lety +1

    you are legend buddy plez plez plez give me your no. i will not disturb you

  • @Bittu-hi4if
    @Bittu-hi4if Před 4 lety

    Bro can we make a app with html css and js

  • @thedev6368
    @thedev6368 Před 3 lety

    i fell asleep and its 6 oclock now

  • @f.o.x.x.2.0
    @f.o.x.x.2.0 Před 3 lety

    else height content > 150px ?)

  • @ahmed-4065
    @ahmed-4065 Před 2 lety

    it doesn't work :(

  • @mdmanik-xy7ui
    @mdmanik-xy7ui Před 3 lety

    I can make it bootstrap very easily

  • @mridulkumar4087
    @mridulkumar4087 Před 3 lety

    Please slow down your video speed it's very fast and removes the background sound next time pls.
    It's very irritating

  • @janemig
    @janemig Před 22 dny

    A fixed height?? Terrible. Anyone know how to make it animate it with varying content? Like in the real world??