mhdi
mhdi

Reputation: 128

Change order of label and input with vanilla js

i have the following form with 2 inputs (or more). i need to change the label style when input:focus and input:valid and to do so i need label to be after input. how can i reorder label and input with pure javascript?!

p.s. i can't change html manually :)

and please don't mind the css below, it includes additional styles i use for other forms on the page i work on

* {
    font-family: Vazir !important;
}
*:focus {
    box-shadow: none !important;
    outline: none !important;
}
.afrm, .logf {
    display: inline-block;
    padding: 3px;
    border-radius: 9px;
    background: linear-gradient( 135deg, #00000000 45%, #F5D000),linear-gradient( 45deg, #00000000 50%, #FFAFCC), linear-gradient( -45deg, #00000000 50%, #DEAAFF), linear-gradient( -135deg, #00000000 40%, #A2D2FF);
}
.afrm form, .logf form {
    padding: 17px;
    border-radius: 8px;
    background: #FFF;
}
.afrm form div {
    position: relative
}
.afrm input, .logf .elementor-form input {
    display: block;
    width: 100%;
    height: 40px;
    background: #FFF;
    border: 2px solid #DFE1E5;
    border-radius: 8px;
    margin-bottom: 12px;
}
.afrm div label, .logf .elementor-form label {
    font-size: 13px;
    line-height: 1em;
    padding: 0 5px;
    position: absolute;
    top: 13px;
    right: 8px;
    pointer-events: none;
    border-radius: 8px;
    transition: 0.2s ease all;
    color: #222 !important;
    z-index: 1;
}
.afrm input:focus ~ label, .afrm input:valid ~ label, .logf .elementor-form .elementor-field-group:focus-within label {
    font-size: 11px;
    top: -5px;
    background: #FFF;
}
.afrmg input:focus, .logf .elementor-form .elementor-field:focus {
    border-color: #F5D000 !important;
    box-shadow: 0 0 0 2px #F5D00040 !important;
}
.afrmg input:valid, .logf .elementor-form input:valid {
    border-color: #0AC974;
    box-shadow: 0 0 0 2px #0AC97440;
}
.passvis {
    position: absolute;
    top: 11px;
    left: 8px;
}
.dce-toggle-password {
    top: 20px !important;
    left: 8px;
    right: auto !important;
}
.mhid:after, .fa-eye-slash:before {
    content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23040505' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24'%3E%3C/path%3E%3Cline x1='1' y1='1' x2='23' y2='23'%3E%3C/line%3E%3C/svg%3E") !important;
}
.mvis:after, .fa-eye:before {
    content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23040505' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z'%3E%3C/path%3E%3Ccircle cx='12' cy='12' r='3'%3E%3C/circle%3E%3C/svg%3E") !important;
}
.afrm p {
    display: block;
}
.afrm p * {
    display: inline-block;
    height: 0;
}
#rememberme {
    appearance: none;
    background-color: 0 0;
    border: 0;
    padding: 0 10px;
    vertical-align: sub;
    width: auto;
}
#rememberme:after {
    content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23222222' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='2' ry='2'%3E%3C/rect%3E%3C/svg%3E");
}
#rememberme:checked {
    background-color: 0 0;
    border: 0;
}
#rememberme:checked:after {
    content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%230ac974' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='9 11 12 14 22 4'%3E%3C/polyline%3E%3Cpath d='M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11'%3E%3C/path%3E%3C/svg%3E");
}
.sgnapr {
    font-size: 12px;
    text-align: right;
}
.sgnapr:before {
    content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23079C59' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'%3E%3C/circle%3E%3Cline x1='12' y1='16' x2='12' y2='12'%3E%3C/line%3E%3Cline x1='12' y1='8' x2='12.01' y2='8'%3E%3C/line%3E%3C/svg%3E");
    margin-left: 5px;
    vertical-align: sub;
}
input[type=submit], .logf .elementor-button {
    width: 100%;
    border: 0;
    background: #001F7C !important;
    font-size: 13px;
    box-sizing: border-box;
    margin: 0;
    border-radius: 8px;
}
input[type=submit]:hover, .logf .elementor-button:hover {
    background: #0AC974 !important;
}
<div class="afrm">
    <form name="registerform" id="registerform" action="" method="post" novalidate="novalidate">
        <div class="afrmg">
            <label for="user_login">username</label>
            <input type="text" name="user_login" id="user_login" class="input" value="" autocapitalize="off" required>
        </div>
        <div class="afrmg">
            <label for="user_email">email</label>
            <input type="email" name="user_email" id="user_email" class="input" value="" required>
        </div>
        <p class="sgnapr">we'll send an email</p>
        <div class="submit">
            <input type="submit" name="wp-submit" id="wp-submit" class="button" value="sign up">
            <input type="hidden" name="redirect_to" value="">
        </div>
    </form>
</div>

thank you!

Upvotes: 1

Views: 433

Answers (1)

dale landry
dale landry

Reputation: 8600

Run the parent elements through a forEach loop then you can use el.appendChild() on the parent Element with el.firstElementChild.

MDN ~ firstElementChild()

//get the parent elements using .querySelectorAll()
const afrmg = document.querySelectorAll(".afrmg");
//run a forEach loop on the nodeList
afrmg.forEach((el) => {
  //append the firstElementChild to the parent node
  el.appendChild(el.firstElementChild);
})
* {
  font-family: Vazir !important;
}

*:focus {
  box-shadow: none !important;
}

.afrm,
.logf {
  display: inline-block;
  padding: 3px;
  border-radius: 9px;
  background: linear-gradient( 135deg, #00000000 45%, #F5D000), linear-gradient( 45deg, #00000000 50%, #FFAFCC), linear-gradient( -45deg, #00000000 50%, #DEAAFF), linear-gradient( -135deg, #00000000 40%, #A2D2FF);
}

.afrm form,
.logf form {
  padding: 17px;
  border-radius: 8px;
  background: #FFF;
}

.afrm form div {
  position: relative
}

.afrm input,
.logf .elementor-form input {
  display: block;
  width: 100%;
  height: 40px;
  background: #FFF;
  border: 2px solid #DFE1E5;
  border-radius: 8px;
  margin-bottom: 12px;
}

.afrm div label,
.logf .elementor-form label {
  font-size: 13px;
  line-height: 1em;
  padding: 0 5px;
  position: absolute;
  top: 13px;
  right: 8px;
  pointer-events: none;
  border-radius: 8px;
  transition: 0.2s ease all;
  color: #222 !important;
  z-index: 1;
}

.afrm input:focus~label,
.afrm input:valid~label,
.logf .elementor-form .elementor-field-group:focus-within label {
  font-size: 11px;
  top: -5px;
  background: #FFF;
}

.afrmg input:focus,
.logf .elementor-form .elementor-field:focus {
  border-color: #F5D000 !important;
  box-shadow: 0 0 0 2px #F5D00040 !important;
}

.afrmg input:valid,
.logf .elementor-form input:valid {
  border-color: #0AC974;
  box-shadow: 0 0 0 2px #0AC97440;
}

.passvis {
  position: absolute;
  top: 11px;
  left: 8px;
}

.dce-toggle-password {
  top: 20px !important;
  left: 8px;
  right: auto !important;
}

.mhid:after,
.fa-eye-slash:before {
  content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23040505' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24'%3E%3C/path%3E%3Cline x1='1' y1='1' x2='23' y2='23'%3E%3C/line%3E%3C/svg%3E") !important;
}

.mvis:after,
.fa-eye:before {
  content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23040505' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z'%3E%3C/path%3E%3Ccircle cx='12' cy='12' r='3'%3E%3C/circle%3E%3C/svg%3E") !important;
}

.afrm p {
  display: block;
}

.afrm p * {
  display: inline-block;
  height: 0;
}

#rememberme {
  appearance: none;
  background-color: 0 0;
  border: 0;
  padding: 0 10px;
  vertical-align: sub;
  width: auto;
}

#rememberme:after {
  content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23222222' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='2' ry='2'%3E%3C/rect%3E%3C/svg%3E");
}

#rememberme:checked {
  background-color: 0 0;
  border: 0;
}

#rememberme:checked:after {
  content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%230ac974' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='9 11 12 14 22 4'%3E%3C/polyline%3E%3Cpath d='M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11'%3E%3C/path%3E%3C/svg%3E");
}

.sgnapr {
  font-size: 12px;
  text-align: right;
}

.sgnapr:before {
  content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23079C59' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'%3E%3C/circle%3E%3Cline x1='12' y1='16' x2='12' y2='12'%3E%3C/line%3E%3Cline x1='12' y1='8' x2='12.01' y2='8'%3E%3C/line%3E%3C/svg%3E");
  margin-left: 5px;
  vertical-align: sub;
}

input[type=submit],
.logf .elementor-button {
  width: 100%;
  border: 0;
  background: #001F7C !important;
  font-size: 13px;
  box-sizing: border-box;
  margin: 0;
  border-radius: 8px;
}

input[type=submit]:hover,
.logf .elementor-button:hover {
  background: #0AC974 !important;
}
<div class="afrm">
  <form name="registerform" id="registerform" action="" method="post" novalidate="novalidate">
    <div class="afrmg">
      <label for="user_login">username</label>
      <input type="text" name="user_login" id="user_login" class="input" value="" autocapitalize="off" required>
    </div>
    <div class="afrmg">
      <label for="user_email">email</label>
      <input type="email" name="user_email" id="user_email" class="input" value="" required>
    </div>
    <p class="sgnapr">we'll send an email</p>
    <div class="submit">
      <input type="submit" name="wp-submit" id="wp-submit" class="button" value="sign up">
      <input type="hidden" name="redirect_to" value="">
    </div>
  </form>
</div>

Will return : enter image description here

Upvotes: 1

Related Questions