Search code examples
javascriptdom-events

Dynamically show/hide div based on first "n" types of input field


I have an input field which filters my divs as the user types, like this

dynamically show/hide div based on the input of textbox

I need an upgrade: the input field length has no limits, but the JavaScript should just consider the first 8 letters/numbers typed by the user and ignore the rest.

For example, on a div like this:

<div class="name">Katekate</div>

If the user types "katekate" or "katekatekate" the result is the same.

I think I should use the "substring" function, but I am getting crazy to solve the problem. Any suggestion would be appreciated!


Solution

  • Your div:

    <div class="name">Katekate</div>
    

    In javascript

    $(".name").val().substr(0, 8);
    

    Result: only the 8 first letters/numbers got caught