<div class="wrapper">
<h3>The <code>trim()</code> method explained</h3>
<input type="text" id="input" value=" Hello, World! " />
<output id="output"></output>
</div>
<a id="read-more" href="">Read more</a>
* {
box-sizing: border-box;
}
body {
margin: 0;
display: grid;
place-items: center;
min-height: 100vh;
font-family: system-ui, sans-serif;
font-size: 1.5rem;
}
.wrapper {
width: min(100% - 2rem, 25rem);
display: grid;
gap: 1rem;
}
h3 {
margin: 0;
font-size: 1.5rem;
text-align: center;
}
input {
font: inherit;
}
output {
border-bottom: 1px solid #ccc;
}
#read-more {
position: absolute;
top: 1rem;
right: 1rem;
display: inline-block;
padding: 0.25em 0.5em;
font-size: 1rem;
text-decoration: none;
text-transform: capitalize;
color: #fff;
background-color: rgb(0, 100, 200);
border-radius: 0.25em;
}
const inputEl = document.getElementById("input");
const outputEl = document.getElementById("output");
inputEl.addEventListener("input", (e) => {
removeExtraSpace(inputEl.value, outputEl);
});
function removeExtraSpace(input, output) {
output.innerText = input.trim();
}
removeExtraSpace(inputEl.value, outputEl);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.