Добавление блока по клику
Не получается добавить элемент. На вкладке "Add more products" при выборе товара он должен добавляться в основной блок но никак. Непосредственно когда основной блок виден новые элементы создаются а когда скрыт не получается. В чем дело и как решить?
const addMore = document.querySelector('.more-title');
const formCheckbox = document.querySelector('.form-checkbox');
const btnContinue = document.querySelector('.form-btn');
const formInner = document.querySelector('.form-inner');
const btnError = document.querySelector('.form-error__btn');
const formBox = document.querySelector('.form-box');
addMore.addEventListener('click', function() {
formInner.style.display = 'none';
formCheckbox.style.display = 'block';
});
btnContinue.addEventListener('click', function() {
formInner.style.display = 'block';
formCheckbox.style.display = 'none';
});
btnError.addEventListener('click', function() {
document.querySelector('.form-error').style.display = 'none';
formInner.style.display = 'block';
});
const addBtn = document.querySelector('.form-item');
const mainBlock = document.getElementById('#body');
addBtn.addEventListener('click', function() {
const createBlock = document.createElement('div');
createBlock.className = 'page-block';
mainBlock.insertAdjacentElement('beforeend', createBlock);
});
.post-content {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: end;
-ms-flex-pack: end;
justify-content: flex-end;
height: 100vh;
}
.post-body {
position: relative;
}
.post-body:after {
position: absolute;
content: url(../../images/bg.svg);
top: -15px;
right: -35px;
}
.post-block {
position: relative;
width: 440px;
height: 535px;
border-radius: 0 50px 50px 50px;
-webkit-box-shadow: 10px 10px 60px #E2E7FF;
box-shadow: 10px 10px 60px #E2E7FF;
background: #FFFFFF;
padding: 35px;
z-index: 1;
}
.form {
position: relative;
height: 100%;
}
.form:before {
position: absolute;
content: '';
width: 100%;
height: 100%;
background: transparent;
z-index: 1;
visibility: hidden;
opacity: 0;
}
.form-box {
position: relative;
border-bottom: 1.5px solid rgba(225, 227, 238, 0.5);
margin: 0 0 25px;
}
.form-box:nth-child(2) {
border: none;
}
.form-box__item {
position: relative;
border-bottom: 1.5px solid rgba(225, 227, 238, 0.5);
margin: 0 0 15px;
}
.form-box__item-keyword,
.form-box__item-link {
width: 100%;
padding: 0 0 4px;
}
#block2,
#block3,
#block4,
#block5 {
display: none;
}
.form-title,
.form-box__title {
font-weight: bold;
font-size: 18px;
line-height: 134%;
color: #1C154E;
margin: 0 0 12px;
}
.form-subtitle,
.form-box__item-subtitle {
font-size: 16px;
line-height: 145%;
color: rgba(28, 21, 78, 0.8);
margin: 0 0 12px;
}
.form-label,
.form-box__item-label {
position: absolute;
top: 76%;
left: 0;
font-size: 16px;
color: #D0D3E2;
-webkit-transition: .4s;
-o-transition: .4s;
transition: .4s;
}
.form-box__item-label {
top: 66%;
}
.form-email {
width: 100%;
}
.form-email:focus+.form-label,
.form-email:focus+.form-box__item-label,
.form-email:valid+.form-label,
.form-email:valid+.form-box__item-label,
.form-box__item-keyword:focus+.form-label,
.form-box__item-keyword:focus+.form-box__item-label,
.form-box__item-keyword:valid+.form-label,
.form-box__item-keyword:valid+.form-box__item-label,
.form-box__item-link:focus+.form-label,
.form-box__item-link:focus+.form-box__item-label,
.form-box__item-link:valid+.form-label,
.form-box__item-link:valid+.form-box__item-label {
-webkit-transform: translateX(100%);
-ms-transform: translateX(100%);
transform: translateX(100%);
visibility: hidden;
opacity: 0;
}
.form-email,
.form-box__item-keyword,
.form-box__item-link {
font-family: inherit;
font-size: 14px;
line-height: 145%;
color: rgba(28, 21, 78, 0.8);
}
.more-title {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
font-weight: 600;
font-size: 18px;
cursor: pointer;
color: #23C967;
margin: 0 0 12px;
}
.more-title__plus {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
width: 13px;
height: 13px;
border-radius: 50%;
border: 1px solid #23C967;
margin: 0 0 0 5px;
font-weight: normal;
font-size: 16px;
}
.more-subtitle {
font-weight: 500;
font-size: 16px;
line-height: 19px;
color: #494471;
margin: 0 0 19px;
}
.form-checkbox {
display: none;
}
.form-checkbox__title {
font-weight: bold;
font-size: 18px;
line-height: 134%;
color: #1C154E;
margin: 0 0 12px;
}
.form-checkbox__subtitle {
font-weight: 500;
font-size: 16px;
line-height: 145%;
color: rgba(28, 21, 78, 0.8);
margin: 0 0 12px;
}
.form-list {
border: 1.5px solid #DDDFED;
border-radius: 25px;
overflow: hidden;
margin: 0 0 23px;
}
.form-item {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
height: 60px;
border-bottom: 1.5px solid #E1E3EE;
padding: 11.5px 17px 10.5px 17px;
}
.form-item:nth-child(5) {
border: none;
}
.form-item {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.form-item:hover {
-webkit-transition: .4s;
-o-transition: .4s;
transition: .4s;
background: #F4F5FA;
}
.form-checkbox {
display: none;
}
.form-checkbox:checked+.form-text:after {
-webkit-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
}
.form-text {
position: relative;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
font-weight: 600;
font-size: 16px;
line-height: 20px;
color: #000000;
}
.form-text span {
font-weight: 500;
font-size: 13px;
line-height: 15px;
color: #23C967;
}
.form-text__item {
width: 100%;
margin: 0 0 0 34px;
}
.form-text:before {
position: absolute;
content: '';
left: 0;
width: 22px;
height: 22px;
border-radius: 50%;
border: 1.5px solid #E1E3EE;
margin: 0 12px 0 0;
}
.form-text:after {
position: absolute;
content: '';
left: 5px;
width: 12px;
height: 12px;
border-radius: 50%;
background: #23C967;
-webkit-transition: .4s;
-o-transition: .4s;
transition: .4s;
-webkit-transform: scale(0);
-ms-transform: scale(0);
transform: scale(0);
}
.form.send .form-button:before,
.form.send .form-button:after,
.form.send:before {
visibility: visible;
opacity: 1;
}
.form-btn {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
height: 45px;
background: #23C967;
border-radius: 15px;
font-weight: bold;
font-size: 18px;
line-height: 22px;
cursor: pointer;
color: #FFFFFF;
}
.form-button {
position: relative;
width: 100%;
height: 45px;
border-radius: 15px;
background: #23C967;
font-weight: bold;
font-size: 18px;
line-height: 22px;
overflow: hidden;
color: #FFFFFF;
}
.form-button:before {
position: absolute;
content: url(../../images/circle.svg);
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 32px;
cursor: default;
margin: auto auto;
z-index: 1;
-webkit-animation-name: rotate;
animation-name: rotate;
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
animation-timing-function: linear;
visibility: hidden;
opacity: 0;
}
.form-button:after {
position: absolute;
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
cursor: default;
background: #23C967;
visibility: hidden;
opacity: 0;
}
@-webkit-keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes rotate {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
.form.send .form-button:before {
visibility: visible;
opacity: 1;
}
.form-secure {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
font-size: 14px;
line-height: 17px;
color: #AFB4CC;
margin: 11px 0 0 0;
}
.form-secure img {
margin: 0 3px 0 0;
}
.form-error {
display: none;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
height: 100%;
padding: 0 0 30px;
}
.form-error__title {
font-weight: bold;
font-size: 18px;
line-height: 134%;
color: #1C154E;
margin: 0 0 12px;
}
.form-error__text {
font-weight: 500;
font-size: 16px;
line-height: 145%;
color: rgba(28, 21, 78, 0.8);
}
.form-error__img {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
height: 100%;
}
.form-error__btn {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-flex: 0;
-ms-flex: 0 0 auto;
flex: 0 0 auto;
height: 45px;
border-radius: 15px;
font-weight: bold;
font-size: 18px;
line-height: 22px;
cursor: pointer;
color: #FFFFFF;
background: #EA717F;
}
.form-send {
display: none;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
height: 100%;
padding: 0 0 30px;
}
.form-send__title {
font-weight: bold;
font-size: 18px;
line-height: 134%;
color: #1C154E;
margin: 0 0 12px;
}
.form-send__text {
font-weight: 500;
font-size: 16px;
line-height: 145%;
color: rgba(28, 21, 78, 0.8);
}
.form-send__img {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
height: 100%;
}
.form-send__btn {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-flex: 0;
-ms-flex: 0 0 auto;
flex: 0 0 auto;
height: 45px;
border-radius: 15px;
font-weight: bold;
font-size: 18px;
line-height: 22px;
cursor: pointer;
color: #FFFFFF;
background: #23C967;
}
<div class="post-body">
<div class="post-block">
<form class="form" id="form" method="" action="#" name="feedback">
<div class="form-inner">
<div class="form-body" id="body">
<div class="form-box">
<div class="form-title">Info</div>
<div class="form-subtitle">Enter your email address</div>
<input class="form-email" id="email" type="text" name="email" required>
<label class="form-label" for="email">[email protected]</label>
</div>
<div class="form-box">
<div class="form-box__title">Product 1</div>
<div class="form-box__item">
<div class="form-box__item-subtitle">Enter main keyword for the product</div>
<input class="form-box__item-keyword" id="keyword" name="keyword" type="text" required>
<label class="form-box__item-label" for="keyword">for example, sylicon wine cup</label>
</div>
<div class="form-box__item">
<div class="form-box__item-subtitle">Enter link to the similar product as a reference</div>
<input class="form-box__item-link" id="link" name="link" type="text" required>
<label class="form-box__item-label" for="link">https://...</label>
</div>
</div>
<div class="form-add more">
<div class="more-title">
<p>Add more products</p>
<div class="more-title__plus">+</div>
</div>
<div class="more-subtitle">
<p>We offer discount up to 36% for multiple checks</p>
</div>
</div>
</div>
<button class="form-button" type="submit">Submit and Pay <span>24.99</span> USD</button>
<div class="form-secure"><img src="images/lock.svg" alt="">
<p>Secure payment with Stripe</p>
</div>
</div>
<div class="form-checkbox">
<div class="form-checkbox__title">Adding more products</div>
<div class="form-checkbox__subtitle">The more items you check, the better the price.</div>
<ul class="form-list">
<li class="form-item">
<input class="form-checkbox" id="product5" type="radio" name="product" value="5">
<label class="form-text" for="product5">
<div class="form-text__item">
<p>5 products for 80 usd / 16$ for each</p><span>You safe 36% on each patent check</span>
</div>
</label>
</li>
<li class="form-item">
<input class="form-checkbox" id="product4" type="radio" name="product" value="4">
<label class="form-text" for="product4">
<div class="form-text__item">
<p>4 products for 72 usd / 18$ for each</p><span>You safe 28% on each patent check</span>
</div>
</label>
</li>
<li class="form-item">
<input class="form-checkbox" id="product3" type="radio" name="product" value="3">
<label class="form-text" for="product3">
<div class="form-text__item">
<p>3 products for 60 usd / 20$ for each</p><span>You safe 20% on each patent check</span>
</div>
</label>
</li>
<li class="form-item">
<input class="form-checkbox" id="product2" type="radio" name="product" value="2">
<label class="form-text" for="product2">
<div class="form-text__item">
<p>2 products for 44 usd / 22$ for each</p><span>You safe 12% on each patent check</span>
</div>
</label>
</li>
<li class="form-item">
<input class="form-checkbox" id="product1" type="radio" name="product" value="1">
<label class="form-text" for="product1">
<div class="form-text__item">
<p>1 product for 24.99 usd</p>
</div>
</label>
</li>
</ul>
<div class="form-btn">Continue</div>
</div>
<div class="form-error">
<div class="form-error__title">Your payment failed</div>
<p class="form-error__text">Sorry, but we’ve having trouble processing your payment. You have been not charged for this transaction.</p>
<div class="form-error__img"><img src="images/card2.png" alt=""></div>
<div class="form-error__btn">Try to pay again</div>
</div>
<div class="form-send">
<div class="form-send__title">Successfully payment</div>
<p class="form-send__text">Your request has been accepted and will be processed within 24 working hours. We will send you a payment details and all information to your email.</p>
<div class="form-send__img"><img src="images/card1.png" alt=""></div>
<div class="form-send__btn">Back</div>
</div>
</form>
</div>
</div>
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
Или везде используйте querySelector, или правильно пишите параметры.
const mainBlock = document.getElementById('body');
^
