Skip to content

Instantly share code, notes, and snippets.

@wunnle
Created January 24, 2017 11:46
Show Gist options
  • Save wunnle/1caffc0f1e90f5fb673d11135959876b to your computer and use it in GitHub Desktop.
Save wunnle/1caffc0f1e90f5fb673d11135959876b to your computer and use it in GitHub Desktop.
Limit total maxlength of two inputs
<input type="text" class="ad1" data-maxlgroup="10" maxlength="10" />
<input type="text" class="ad2" data-maxlgroup="10"
maxlength="10"/>
var maxLgroup = function () {
let maxTotalL;
if($("[data-maxlgroup]").length) {
maxTotalL = $("[data-maxlgroup]").data("maxlgroup")
}
$("[data-maxlgroup]").change(function() {
let activeL = $(this).val().length
let leftoverL = maxTotalL - activeL
console.log(activeL, "activeL")
console.log(leftoverL, "leftoverL")
$("[data-maxlgroup]").not($(this)).attr("maxlength", leftoverL)
})
}
$(function() {
maxLgroup();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment