;(function(ns){
"use strict";
//REM: Makes an input-element flexible to its width
function _makeFlex(input){
if(input && input.type){
//REM: Wrapping the input-element with a label-element (makes the most sense on inputs)
_wrapInputInLabel(input);
//REM: Adding a listener to inputs, so that the content of othe ::after can be adjusted
input.addEventListener('input', _onInput, false);
}
};
function _onInput(){
var tInput = this,
tParent = tInput.parentNode;
//REM: Just verifying.. better save than sorry :-)
if(tInput.type && tParent.tagName === 'LABEL' && tParent.getAttribute('data-flex')){
//REM: Here exceptions can be set for different input-types
switch(tInput.type.toLowerCase()){
case 'password':
//REM: This one depends on the browser and/or OS
//https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/password
tParent.setAttribute('data-flex-content', tInput.value.replace(/./g, '•'))
break
default:
tParent.setAttribute('data-flex-content', tInput.value)
}
}
};
//REM: Wraps the input in a label-element
function _wrapInputInLabel(input){
if(input){
var tLabel = (input.parentNode && input.parentNode.tagName === 'LABEL') ? input.parentNode : input.parentNode.appendChild(document.createElement('label'));
tLabel.setAttribute('data-flex', input.getAttribute('data-flex'));
tLabel.appendChild(input);
//REM: Copy the font-styles on the label - can be expanded with more
tLabel.style.fontFamily = window.getComputedStyle(input, null).getPropertyValue('font-family');
tLabel.style.fontSize = window.getComputedStyle(input, null).getPropertyValue('font-size');
if(input.id){
tLabel.setAttribute('for', input.id)
}
}
};
ns.Flex = {
Init: function(){
//REM: Loops through all the marked input elements
for(let tL=document.querySelectorAll('input[data-flex]'), i=0, j=tL.length; i<j; i++){
_makeFlex(tL[i])
}
}
}
})(window.mynamespace=window.mynamespace || {});
;window.onload = function(){
mynamespace.Flex.Init()
};
label[data-flex]{
font-family: arial;
font-size: 13px;
display: inline-block;
min-width: 30px;
position: relative
}
label[data-flex]::after{
background: red;
border: 1px solid black;
content: attr(data-flex-content);
display: block;
visibility: hidden
/*REM: following styles are just for demo purposes */
line-height: 20px;
visibility: visible;
}
/*REM: Has a little slider on the right */
label[data-flex='number']::after{
margin-right: 18px
}
label[data-flex] > input{
font-size: 13px;
font-family: arial;
width: 100%
}
<input type = 'text' placeholder = 'text' data-flex = 'flex' id = 'inText' />
<input type = 'text' placeholder = 'crazy' data-flex = 'flex' id = 'inText2' style = 'font-size: 20px; font-family: comic-sans' />
<input type = 'password' placeholder = 'password' data-flex = 'flex' id = 'inPassword' />
<label>
<input type = 'number' placeholder = 'number' data-flex = 'number' id = 'inNumber' />
</label>