Reputation: 169
On this page https://www.startwithsquare1.com/signup/ When you click on any of the fields it adds "-top" to the "sq-input sq-input_light -inverse" class. Is there a way to add this without javascript? If I do have to use JS, how would I do it? I am trying to achieve how the label scales up when you click on the field.
Thanks!!
Upvotes: 0
Views: 2781
Reputation: 119
I think such manipulations are bad practice. They are always feels like a crutch.
If you are dont want to waste your time on javascript, then try Elemental, it is pretty easy to use, and it's 3kb only.
Your page gives 503 error, so i will just try to make an example.
var element = document.querySelector('.element');
element.addEventListener('click', function () {
Elemental.add(this, 'myNewCssClass');
})
It is easy to use, and it works fast
Upvotes: 0
Reputation: 170
You can easy do it with HTML and CSS code. This is called Floating Placeholder and you need to:
Here's the example and at the end of the answer is the link to a Video Tutorial if you have any more doubt, but the main thing is the class:focus
.
body
{
margin: 0;
padding: 0;
font-family: sans-serif;
background: url(college.png);
background-repeat: no-repeat;
background-size: cover;
}
.container
{
position: absolute;
top: 50%;
left: 75%;
transform: translate(-50%,-50%);
width: 700px;
background: #fff;
padding: 45px;
box-sizing: border-box;
border :1px solid rgba(0,0,0,.1);
box-shadow: 0 5px 10px rgba(0,0,0,.2);
}
.container h1
{
text-align: center;
margin: 0 0 40px;
padding: 0;
color: rgb(66,133,244);
letter-spacing: 2px;
}
.container input
{
padding: 5px;
margin-bottom: 30px;
width: 55%;
box-sizing: border-box;
box-shadow: none;
outline: none;
border:none;
border-bottom: 2px solid #999;
}
.container input[type="submit"]
{
border-radius: 25px;
font-size: 20px;
height: 40px;
cursor : pointer;
background: rgb(66,133,244);
color: #fff;
margin-bottom: 0;
}
.container input[type="submit"]:hover
{
background: #34F458;
color: #fff;
}
.container form div
{
position: relative;
}
.container form div label
{
position: absolute;
top: 3px;
pointer-events: none;
left: 0;
transition: .5s;
}
.container input:focus ~label,
.container input:valid ~label
{
left: 0;
top: -20px;
color: rgb(66,133,244);
font-size: 12px;
font-weight: bold;
}
.container input:focus,
.container input:valid
{
border-bottom: 2px solid rgb(66,133,244);
}
<html>
<body>
<div class="container">
<form>
<div>
<input type="email" name="" required="">
<label>Email id</label>
</div>
<div>
<input type="password" name="" required="">
<label>Password</label>
</div>
<div>
<input type="password" name="" required="">
<label>Re-Enter Password</label>
</div>
<input type="submit" value="Register" name="">
</form>
</div>
</body>
</html>
VIDEO TUTORIAL: https://youtu.be/xTShp-tZbD0
Upvotes: 2
Reputation: 29
Following is the HTML -
<div id="div1" class="abc">
<....inside tags...>
</div>
Following is the js -
var x = document.getElementById("div1");
x.className += " otherclass";
Otherclass is is the name of the class added.
you can also refer to https://developer.mozilla.org/en-US/docs/Web/API/Element/className
Upvotes: 1
Reputation: 465
You need javascript to do this. Function example:
function addClass() {
var element = document.getElementsByClassName("sq-input");
element.classList.add("-top");
}
For more information: https://www.w3schools.com/howto/howto_js_add_class.asp
Upvotes: 0
Reputation: 960
you can use jquery to do so
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p:first").addClass("intro");
});
});
</script>
<style>
.intro {
font-size: 45px;
color: blue;
text-decoration:underline;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Add a class name to the first p element</button>
</body>
</html>
Upvotes: 0