PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 9, 2022

[FIXED] How to write this css and javascript better

 November 09, 2022     css, html, javascript     No comments   

Issue

I wanted to know how this piece of code can be improved.

I don't know how well written it is to begin with.

How it can be written better.

Is there anything here you would write a different way?

code https://jsfiddle.net/7keLvajm/

.container2 {
  display: none;
}

.container2.active  {
  display: flex;
}

.container1.active2  {
 display: none;
}

  function resetBackground(backgroundSelector) {
    const background = document.querySelector(backgroundSelector);
    background.classList.add("bg1");
  }

  function resetContainer1(containerSelector) {
    const container = document.querySelector(containerSelector);
    container.classList.add("active");
  }

  function resetContainer2(containerSelector2) {
    const allContainers = document.querySelectorAll(containerSelector2);

    function hideContainer(container1) {
      container1.classList.add("active2");
    }
    allContainers.forEach(hideContainer);
  }

  function resetPage() {
    resetBackground("body");
    resetContainer1(".container2");
    resetContainer2(".container1");
  }

Solution

Your code seems pretty good how it is, the only thing that I would want to draw attention to is your resetContainer2() function:

function resetContainer2(containerSelector2) {
    const allContainers = document.querySelectorAll(containerSelector2);

    function hideContainer(container1) {
      container1.classList.add("active2");
    }
    allContainers.forEach(hideContainer);
}

You are declaring a function inside of a function, which means it will only be able to be used inside resetContainer2(). If you're okay with that, then your code seems good!



Answered By - Anye
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing