Display of units in HTML stepper <input type="number">

Simon picture Simon · Feb 19, 2015 · Viewed 10k times · Source

I want the user to enter numbers with a unit such as "cm", "kg" or "$". This can be done in jQuery UI: Example

enter image description here

However, I would like to implement it in pure html, such as:

Is there a more native way for doing it (like example 2) or do I have to implement the workaround (example 3)?

Answer

N K picture N K · Mar 26, 2015

$(".original input").on("change", function(){
    $(".duplicate input").val(this.value + '€');
});
$(".duplicate input").on("change", function(){
    $(".original input").val(this.value.substring(0, this.value.length - 1));
});
.duplicate {
    position: relative;
    top: -20px;
    left: 2px;
    float: left;
}
.duplicate input {
    width: 145px;
    border: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="euro-sign">
    <div class="original">
        <input type="number" step="1"/>
    </div>
    <div class="duplicate">
        <input type="text" value="0€"/>
    </div>
</div>