Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep label minimized at the top of input field once text has been entered?

I can't figure out how to keep the label minimized at the top once text has been entered into the form.

Can anyone help?

http://codepen.io/cassieevans/pen/Vjqdba

<form id="form">

    <fieldset>
        <input id="name" name="" type="text"/>
        <label for="name">NAME</label>
    </fieldset>

    <fieldset>
        <input id="email" name="" type="email"/>
        <label for="email">EMAIL</label>
    </fieldset>

    <fieldset>
        <textarea id="message" name="" type="text"></textarea>
        <label for="message">MESSAGE</label>
    </fieldset>

like image 263
Cassie Evans Avatar asked Oct 29 '22 22:10

Cassie Evans


2 Answers

Your code works perfectly. You just forgot to add jQuery.

$('input,textarea').on('blur', function(){
  if( !$(this).val() == "" ){
    $(this).next().addClass('stay');
  } else {
    $(this).next().removeClass('stay');
  }
});

Also you should add textarea to your selector so it works there too.

like image 83
Juan Ferreras Avatar answered Nov 15 '22 05:11

Juan Ferreras


EDIT V2:

Here's another version, it includes symbols if :valid or :invalid.


CODE SNIPPET:

//Contact Inputs
(function() {
  var inputs = $("input,textarea"),
    sFieldSetNotEmpty = "not-empty",
    sFieldSetError = "has-error",
    sFieldSetSuccess = "has-success";

  inputs.each(function() {
    var $this = $(this),
      $thisFieldSet = $this.parent("fieldset");

    $this
      .on("change", function() {
        if ($this.is(":valid")) {
          $thisFieldSet
            .removeClass(sFieldSetError)
            .addClass(sFieldSetSuccess);
        } else if ($this.is(":invalid")) {
          $thisFieldSet
            .removeClass(sFieldSetSuccess)
            .addClass(sFieldSetError);
        }
        if ($(this).val()) {
          $thisFieldSet.addClass(sFieldSetNotEmpty);
        } else {
          $thisFieldSet.removeClass(sFieldSetNotEmpty);
        }
      });
  });
})();
body {
  background-color: #191a1a;
  margin: 0;
  padding: 0;
  font-family: 'Raleway', sans-serif;
}
#form {
  width: 50%;
  margin: 0 auto;
}
fieldset {
  border: none;
  position: relative;
}
input {
  font-size: 1.2em;
  width: 100%;
  height: 50px;
  border: 1px solid #aaa;
  border-radius: 2px;
  padding: 1em 1em .8em 1em;
  position: relative;
  background: transparent;
  color: #008080;
}
fieldset::after {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 0;
  font-size: 1.5em;
  pointer-events: none;
}
fieldset.has-success::after {
  content: "✓";
  color: greenyellow;
}
fieldset.has-error::after {
  content: "X";
  color: tomato;
}
input:focus {
  outline: none;
  background: transparent;
}
input + label {
  display: block;
  cursor: text;
  color: #777;
  transition: .15s ease-out all;
  position: absolute;
  top: 1.8em;
  left: 2.3em;
}
input:focus + label,
.not-empty label {
  top: 1em;
  left: 3em;
  font-weight: bold;
  font-size: .7em;
  color: #008080;
  transition: .15s ease-out all;
}
fieldset.has-success label {
  color: greenyellow;
}
fieldset.has-error label {
  color: tomato;
}
textarea {
  font-size: 1.2em;
  width: 100%;
  height: 200px;
  border: 1px solid #aaa;
  border-radius: 2px;
  padding: 1em 1em .8em 1em;
  background: transparent;
  outline: none;
  color: #008080;
}
textarea:focus {
  outline: none;
  background: transparent;
}
textarea + label {
  display: block;
  cursor: text;
  color: #777;
  transition: .15s ease-out all;
  position: absolute;
  top: 1.8em;
  left: 2.3em;
}
textarea:focus + label,
.not-empty label {
  top: 1em;
  left: 3em;
  font-family: 'Raleway', sans-serif;
  font-weight: bold;
  font-size: .7em;
  color: #008080;
  transition: .15s ease-out all;
}
#submit {
  border: 2px solid #FFFFFF;
  border-radius: 300px;
  width: 20%;
  outline: none;
  cursor: pointer;
  margin: auto;
  display: block;
  margin-top: 20px;
  font-family: 'Raleway', sans-serif;
  color: #FFFFFF;
  padding: 10px;
}
#submit:hover {
  transition: background-color 1s ease;
  background-color: #008080;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">

  <fieldset>
    <input id="name" name="" type="text" required/>
    <label for="name">NAME</label>
  </fieldset>

  <fieldset>
    <input id="email" name="" type="email" required/>
    <label for="email">EMAIL</label>
  </fieldset>

  <fieldset>
    <textarea id="message" name="" type="text" required></textarea>
    <label for="message">MESSAGE</label>
  </fieldset>

  <input id="submit" type="submit" value="GO!">
</form>

V1:

Your code seems fine, you just needed to reference jQuery.

I made some changes to your code and added validation using the CSS pseudo-classes :valid and :invalid.

This will help you have a better UX.


CODE SNIPPET:

//Contact Inputs
(function() {
  var inputs = $("input,textarea"),
    sLabelNotEmpty = "stay",
    sLabelError = "label--error",
    sLabelSuccess = "label--success";

  inputs.each(function() {
    var $this = $(this),
      $thisLabel = $this.next("label");

    $this
      .on("change", function() {
        if ($this.is(":valid")) {
          $thisLabel
            .removeClass(sLabelError)
            .addClass(sLabelSuccess);
        } else if ($this.is(":invalid")) {
          $thisLabel
            .removeClass(sLabelSuccess)
            .addClass(sLabelError);
        }
        if ($(this).val()) {
          $thisLabel.addClass(sLabelNotEmpty);
        } else {
          $thisLabel.removeClass(sLabelNotEmpty);
        }
      });
  });
})();
body {
  background-color: #191a1a;
  margin: 0;
  padding: 0;
  font-family: 'Raleway', sans-serif;
}
#form {
  width: 50%;
  margin: 0 auto;
}
fieldset {
  border: none;
  position: relative;
}
input {
  font-size: 1.2em;
  width: 100%;
  height: 50px;
  border: 1px solid #aaa;
  border-radius: 2px;
  padding: 1em 1em .8em 1em;
  background: transparent;
  color: #008080;
}
input:focus {
  outline: none;
  background: transparent;
}
input + label {
  display: block;
  cursor: text;
  color: #777;
  transition: .15s ease-out all;
  position: absolute;
  top: 1.8em;
  left: 2.3em;
}
input:focus + label,
.stay {
  top: 1em;
  left: 3em;
  font-weight: bold;
  font-size: .7em;
  color: #008080;
  transition: .15s ease-out all;
}
label.label--error {
  color: tomato;
}
label.label--success {
  color: greenyellow;
}
textarea {
  font-size: 1.2em;
  width: 100%;
  height: 200px;
  border: 1px solid #aaa;
  border-radius: 2px;
  padding: 1em 1em .8em 1em;
  background: transparent;
  outline: none;
  color: #008080;
}
textarea:focus {
  outline: none;
  background: transparent;
}
textarea + label {
  display: block;
  cursor: text;
  color: #777;
  transition: .15s ease-out all;
  position: absolute;
  top: 1.8em;
  left: 2.3em;
}
textarea:focus + label,
.stay {
  top: 1em;
  left: 3em;
  font-family: 'Raleway', sans-serif;
  font-weight: bold;
  font-size: .7em;
  color: #008080;
  transition: .15s ease-out all;
}
#submit {
  border: 2px solid #FFFFFF;
  border-radius: 300px;
  width: 20%;
  outline: none;
  cursor: pointer;
  margin: auto;
  display: block;
  margin-top: 20px;
  font-family: 'Raleway', sans-serif;
  color: #FFFFFF;
  padding: 10px;
}
#submit:hover {
  transition: background-color 1s ease;
  background-color: #008080;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">

  <fieldset>
    <input id="name" name="" type="text" required/>
    <label for="name">NAME</label>
  </fieldset>

  <fieldset>
    <input id="email" name="" type="email" required/>
    <label for="email">EMAIL</label>
  </fieldset>

  <fieldset>
    <textarea id="message" name="" type="text" required></textarea>
    <label for="message">MESSAGE</label>
  </fieldset>

  <input id="submit" type="submit" value="GO!">
</form>
like image 29
Ricky Avatar answered Nov 15 '22 05:11

Ricky