How can I limit my selector to a specific element related to an input?

Kyle Underhill

I want the character counting code to work on multiple inputs but I can't figure out how to implement an each function on my keyup so that the .char-count works on the closest .input

function checkTextAreaMaxLength(textBox, e) {
  var maxLength = parseInt($(textBox).data("length"));
  if (!checkSpecialKeys(e)) {
    if (textBox.value.length > maxLength - 1)
      textBox.value = textBox.value.substring(0, maxLength);
  }
  $(".char-count").html(maxLength - textBox.value.length);
  return true;
}
function checkSpecialKeys(e) {
  if (
    e.keyCode != 8 &&
    e.keyCode != 46 &&
    e.keyCode != 37 &&
    e.keyCode != 38 &&
    e.keyCode != 39 &&
    e.keyCode != 40
  )
    return false;
  else return true;
}

$(".input").on("keyup", function(event) {
  checkTextAreaMaxLength(this, event);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="item">
    <textarea class='input' rows='3' data-length=250 data-min-rows='3'></textarea>
    <h4><span class="char-count">250</span> chars remaining</h4>
</div>

<div class="item">
    <input class='input' data-length=120/>
    <h4><span class="char-count">120</span> chars remaining</h4>
</div>

isherwood

Just reduce your selector using DOM traversal. There are many ways to do this. Here's one approach using an ancestor element:

$(textBox).closest('.item').find('.char-count').html(maxLength - textBox.value.length);

Another would be to use a sibling selector:

$(textBox).siblings().find('.char-count').html(maxLength - textBox.value.length);

Or, more specifically, the next element selector:

$(textBox).next().find('.char-count').html(maxLength - textBox.value.length);

I like to be a little more specific with the ancestor selector in case the interior markup structure changes. parent() is fragile sometimes. If you were to add a wrapper around the input for layout purposes, for example (as in when using Bootstrap), parent() is no longer appropriate and your script breaks. I find it more durable to target the outermost element in your markup group. The same idea is true of siblings() and next(). It's better to leave some flexibility in your selector.

function checkTextAreaMaxLength(textBox, e) {
var maxLength = parseInt($(textBox).data("length"));

if (!checkSpecialKeys(e)) {
    if (textBox.value.length > maxLength - 1)
        textBox.value = textBox.value.substring(0, maxLength);
}

$(textBox).closest('.item').find('.char-count').html(maxLength - textBox.value.length);

return true;
}

function checkSpecialKeys(e) {
if (
    e.keyCode != 8 &&
    e.keyCode != 46 &&
    e.keyCode != 37 &&
    e.keyCode != 38 &&
    e.keyCode != 39 &&
    e.keyCode != 40
)
    return false;
else return true;
}

$(".input").on("keyup", function(event) {
checkTextAreaMaxLength(this, event);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
<textarea class='input' rows='3' data-length=250 data-min-rows='3'></textarea>
<h4><span class="char-count">250</span> chars remaining</h4>
</div>
<div class="item">
<input class='input' data-length=120></input>
<h4><span class="char-count">120</span> chars remaining</h4>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I limit the expanding of my input?

How can I use a ::before selector at the bottom of my element?

How do I limit my user input

How I can access DOM element by selector

How can I make jQuery find the element if the string placement is random in my selector?

How can I limit the amount for input? Powershell

How can I limit the user input in quantity?

How can I optimize my code for Swapping the array elements of given range of indexes with related element?

How can I refine my Python reverse image search to limit to a specific domain?

How can I limit my powerset function?

Python & Selenium: How can I obtain a href element from a specific div? My element is stale

How can i limit my blogs per page on my website?

How can I get to some specific text using selector?

How can I accurately locate a web element in a web page with selector

How can I extract a specific part of an input?

How can I remove the last two characters of a specific element in my list?

How can I change the placeholder color from my el-input with Element UI

How can I print specific element

How can I detect specific element is clicked?

How can I check a checkbox for a specific element?

How can I find a specific element with XPath?

How can I locate an element with specific text in it?

How can I make my function a public selector?

Can I get my code to replay after a specific input?

How can I public the element in my question

How can I limit Bootstrap column to a specific column's height?

How can I limit PHP code to a specific folder?

How can I limit the winston log files to a specific number

How can I limit the result to a field with array that has a specific value?