when a user enters a hex number for example b1b1b1, add the # symbol if he forgot to write. html:
<input type="text" class="color-scheme__input-color"> when a user enters a hex number for example b1b1b1, add the # symbol if he forgot to write. html:
<input type="text" class="color-scheme__input-color"> As an option (IE 11+):
let input = document.querySelector("input"); input.addEventListener("change", function() { if (this.value[0] != '#') { this.value = '#' + this.value; } }); <input type="text"> Need a jQuery translation?
UPD
$("input").on("change", function() { let $me = $(this); if ($me.val()[0] != "#") { $me.val( "#" + $me.val() ) } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text"> Source: https://ru.stackoverflow.com/questions/715397/
All Articles