Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jason limmm
jason limmm
8,009 Points

anything wrong with this js code?

here is my snapshot:https://w.trhou.se/3nsbahv8qt

i tried to make it so that if the order number is a certain number it'd display a stage of the form. it works but after i click the previous button the margin of the next button gets messed up.

//make the variables for stuff for convenience
const nextbutton = document.body.querySelector(".next");
const previousbutton = document.body.querySelector(".previous");
const progressbarjuice = document.body.querySelector(".bar .myval");
const inputname = document.body.querySelector("#nameinput");
const emailinput = document.body.querySelector("#emailinput");
const inputphonenumber = document.body.querySelector('#inputphonenumber')
const percentincrement = 33.333;
let ordernumber=1;
let placeholder='';
function condition(){
    if (ordernumber===1){
        placeholder=inputname;
    }else if(ordernumber===2){
        placeholder=emailinput;
        otherplaceholder=inputname;
    }else if(ordernumber===3){
        placeholder=inputphonenumber;
        otherplaceholder=emailinput;
    }
}

nextbutton.addEventListener(
  "click", ()=>{
    ordernumber+=1;
    condition();
    progressbarjuice.style.width += `${percentincrement}%`;
    otherplaceholder.style.display="none";
    placeholder.style.display="block";
  }
)

previousbutton.addEventListener(
  "click", ()=>{
    condition();
    ordernumber-=1;
    otherplaceholder.style.display="block";
    placeholder.style.display="none";
  }
)

here is the code

1 Answer

Steven Parker
Steven Parker
231,275 Points

The button margins don't change, but the width of the flex form div does. This shifts the button position.

I notice there is quite a bit of redundancy in the page layout. This is a great opportunity to practice the DRY principle ("Don't Repeat Yourself"). For example, having just one control that you adjust properties on (compared to multiple copies that become visible and invisible) could allow for code that would be much less likely to exhibit mystery behaviors such as this.

You might benefit by setting this project aside for a while and taking a few more of the courses. I know some of them teach DRY design, DOM manipulation, and various methods of styling and layout which would all assist your project development.

jason limmm
jason limmm
8,009 Points

can you suggest to me some courses?

Steven Parker
Steven Parker
231,275 Points

I don't know which you may have already taken, but the system will mark any you have done so you don't need to repeat them. These course tracks all contain information you might find useful (I certainly did):