Не меняется цвет кнопки после валидации

Рейтинг: -1Ответов: 1Опубликовано: 20.05.2023

let burger = document.getElementById("burger");
let name = document.getElementById("name_1");
let phone = document.getElementById("phone");

document.getElementById("order-action").onclick = function() {

  let hasError = false;

  [burger, name_1, phone].forEach(item => {
    if (item.value === "") {
      document.getElementById("order-action").style.background = "gray";
      item.parentElement.style.background = "red";

      hasError = true;

    } else {
      document.getElementById("order-action").style.background = "";
      item.parentElement.style.background = "";

    }
  });
  if (!hasError) {
    [burger, name, phone].forEach(item => {
      item.value = "";
    });
    alert("Спасибо за заказ! Мы скоро свяжемся с вами!");
  }

}
* {
  margin: 0;
  padding: 0;
}

@font-face {
  font-family: 'Gilroy';
  src: url('../fonts/Gilroy-Semibold.ttf');
}

@font-face {
  font-family: 'Merriweather';
  src: url('../fonts/Merriweather-Black.ttf');
}

body {
  background-color: #191411;
  font-family: 'Gilroy', 'sans-serif';
  color: white;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
}

.button {
  font-family: 'Gilroy', 'sans-serif';
  font-weight: 600;
  line-height: 22px;
  font-size: 18px;
  background: linear-gradient(95.61deg, #DA8023 0%, #E2B438 100%);
  border-radius: 8px;
  width: 260px;
  height: 62px;
  box-sizing: border-box;
  color: #191411;
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 0;
  cursor: pointer;
  outline: none;
}

.button:hover {
  background: linear-gradient(95.61deg, #AC6115 0%, #9E7A19 100%);
}

.header {
  padding: 40px 0;
}

.header .container {
  display: flex;
  align-items: center;
}

.logo img {
  vertical-align: bottom;
}

.menu {
  margin-left: 191px;
}

.menu-list {
  list-style: none;
  display: flex;
}

.menu-item {
  margin-right: 88px;
}

.menu-item a {
  font-size: 18px;
  color: #fff;
  text-decoration: none;
  cursor: pointer;
}

.menu-item a:hover {
  border-bottom: 2px solid #fff;
}

.currency {
  border: 1px solid #fff;
  border-radius: 5px;
  width: 45px;
  height: 45px;
  padding: 6px;
  box-sizing: border-box;
  text-align: center;
  cursor: pointer;
  line-height: 32px;
  margin-left: auto;
  user-select: none;
}

.main {
  overflow: hidden;
  background-image: url('../images/main_bg.png');
  background-position: top center;
  background-repeat: no-repeat;
}

.main-content {
  padding-top: 103px;
  padding-bottom: 199px;
}

.main-content .container {
  position: relative;
}

.main-info {
  max-width: 608px;
}

.main-small-info {
  background: #d3320f;
  border-radius: 100px;
  padding: 12px;
  display: inline-block;
  font-size: 18px;
}

.main-title {
  font-family: 'Merriweather', 'sans-serif';
  font-size: 120px;
  line-height: 156px;
  text-transform: uppercase;
}

.main-text {
  margin-top: 56px;
  max-width: 420px;
  margin-bottom: 56px;
}

.main-image {
  position: absolute;
  top: -123px;
  left: calc(100% - 991px);
}

.why {
  background-image: url('../images/whybg.png');
  background-position: top center;
  background-size: initial;
  padding-bottom: 180px;
}

.common-title {
  font-family: 'Merriweather', 'sans-serif';
  font-style: normal;
  font-weight: 900;
  font-size: 64px;
  line-height: 80px;
  text-align: center;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.why-items {
  margin-top: 60px;
  display: grid;
  gap: 100px;
  grid-template-columns: repeat(3, 312px);
  justify-content: center;
}

.why-item {
  text-align: center;
}

.why-item-title {
  font-weight: 600;
  font-size: 24px;
  line-height: 29px;
  padding: 24px 0;
}

.why-item-text {
  font-weight: 600;
  font-size: 16px;
  line-height: 140%;
}

.products {
  background-image: url('../images/burgers_bg.png');
  background-position: top center;
  background-size: 1400px;
  padding-bottom: 180px;
}

.products-items {
  display: grid;
  grid-template-columns: repeat(3, 384px);
  gap: 24px;
  margin-top: 60px;
}

.products-item {
  padding: 30px;
  background: #211A16;
  border: 1px solid #353535;
  border-radius: 16px;
}

.products-item-title {
  font-family: 'Gilroy', 'sans-serif';
  font-style: normal;
  font-weight: 600;
  font-size: 24px;
  line-height: 29px;
}

.products-item-text {
  font-family: 'Gilroy', 'sans-serif';
  font-style: normal;
  font-weight: 600;
  font-size: 16px;
  line-height: 140%;
  margin-bottom: 24px;
  margin-top: 24px;
}

.products-item-extra {
  display: flex;
  justify-content: space-between;
}

.products-item-price {
  font-size: 32px;
  line-height: 39px;
  margin-bottom: 8px;
}

.products-item-weight {
  font-size: 16px;
  line-height: 18px;
  color: #757575;
}

.button.products-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 182px;
  height: 62px;
}

.button.products-button span:first-child {
  margin-right: 10px;
}

.order {
  background-image: url('../images/order_bg.png');
  background-position: top center;
  background-repeat: no-repeat;
  padding-bottom: 180px;
  overflow: hidden;
}

.order .container {
  position: relative;
}

.order-image {
  position: absolute;
  top: 0;
  right: calc(100% - 764px);
  z-index: -1;
}

.order-form {
  background: #211A16;
  border: 1px solid #353535;
  border-radius: 16px;
  margin-top: 60px;
  max-width: 426px;
  box-sizing: border-box;
  padding: 60px 40px;
  margin-left: 674px;
}

.order-form-text {
  text-align: center;
  font-size: 24px;
  line-height: 29px;
}

.order-form-inputs {
  display: flex;
  flex-direction: column;
  margin-top: 40px;
}

.order-form-input {
  background: linear-gradient(95.61deg, #DA8023 0%, #E2B438 100%);
  width: 344px;
  height: 62px;
  border-right: 8px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.order-form-input input {
  padding: 20px 10px;
  background: #211A16;
  border-radius: 8px;
  width: 342px;
  height: 60px;
  box-sizing: border-box;
  outline: none;
  border: 1px solid transparent;
  color: #fff;
}

.order-form-inputs .button {
  width: 100%;
  height: 62px;
}

.footer {
  padding: 60px 0;
}

.footer .container {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rights {
  font-size: 16px;
  line-height: 20px;
}
<section class="order">
  <div class="container">
    <div class="order-title common-title" id="order">оформление заказа</div>
    <img src="./images/order_image.png" alt="burger in the box" class="order-image">
    <div class="order-form">
      <div class="order-form-text">Заполните все данные и наш менеджер свяжется с вами для подтверждения заказа
      </div>
      <div class="order-form-inputs">
        <div class="order-form-input"><input type="text" placeholder="Ваш заказ" id="burger"></div>
        <div class="order-form-input"><input type="text" placeholder="Ваше имя" id="name_1"></div>
        <div class="order-form-input"><input type="text" placeholder="Ваш телефон" id="phone"></div>
        <button class="button" id="order-action">Оформить заказ</button>
      </div>
    </div>
  </div>
</section>

Ответы

▲ 0

Вам надо убрать из цикла окрас кнопки. Поскольку при последней итерации, если поле не пустое, у вас и происходит сброс кнопки к дефолтному цвету, вне зависимости от того, что было до этого. У вас есть переменная hasError, от неё и отталкивайтесь. После цикла проверяйте.

if (hasError) { 
   document.getElementById("order-action").style.background = "gray";
}

И соответстенно возвращайте кнопке дефолтное значение, если !hasError