"Floating Labels in 3 lines of jQuery"
Bootstrap 3.2.0 Snippet by Siraj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="[ container ]">
<div class="[ row ]">
<h2 class="[ text-center ]">Floating Labels with only 3 Lines of jQuery</h2>
</div>
</div>
<div class="[ container ]">
<div class="[ row ]">
<div class="[ col-xs-12 col-sm-offset-2 col-sm-8 col-md-offset-4 col-md-4 ]">
<div class="[ form-group ]">
<input id="floatingLabels" name="floatingLabels" placeholder="Floating Labels" class="[ form-control ]" data-toggle="floatLabel" data-value="no-js">
<label for="floatingLabels" style="">Floating Labels</label>
</div>
</div>
</div>
<div class="[ row ]">
<div class="[ col-xs-12 col-sm-offset-2 col-sm-8 col-md-offset-4 col-md-4 ]">
<div class="[ form-group ]">
<input id="floatingLabelsRequired" name="floatingLabelsRequired" placeholder="Floating Labels Required" class="[ form-control ]" required data-toggle="floatLabel" data-value="no-js">
<label for="floatingLabelsRequired" style="">Floating Labels Required</label>
</div>
</div>
</div>
<div class="[ row ]">
<div class="[ col-xs-12 col-sm-offset-2 col-sm-8 col-md-offset-4 col-md-4 ]">
<div class="[ form-group ][ form-group-textarea ]">
<textarea id="customStyle" name="customStyle" placeholder="Custom Style" class="[ form-control ]" data-toggle="floatLabel" data-value="no-js"></textarea>
<label for="customStyle" style="">Custom style</label>
</div>
</div>
</div>
</div>
<div class="[ container ]">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Not Need */
@import url(http://fonts.googleapis.com/css?family=Roboto:500);
body { background-color: rgb(230, 235, 240); }
/* Basic Style */
.form-group {
position: relative;
}
.form-group [data-toggle="floatLabel"] {
height: 44px;
padding-top: 16px;
}
.form-group [data-toggle="floatLabel"] + label {
font-size: 12px;
left: 12px;
opacity: 1;
position: absolute;
top: 3px;
transition: all 0.3s ease-in-out;
}
.form-group [data-toggle="floatLabel"]:required + label {
color: rgb(255, 0, 0);
}
/* Custom Styles */
.form-group.form-group-textarea {
background-color: rgb(255, 255, 255);
border-radius: 1px;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
margin: 20px 15px ;
padding: 10px 0px 2px;
position: relative;
}
.form-group.form-group-textarea textarea {
height: 34px;
resize: none;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
$(document).ready(function(){
// Floating Labels
//==============================================================
$('[data-toggle="floatLabel"]').attr('data-value', $(this).val()).on('keyup change', function() {
$(this).attr('data-value', $(this).val());
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments: