P
P
Pavel Voronyuk2018-03-01 10:57:24
JavaScript
Pavel Voronyuk, 2018-03-01 10:57:24

How can the script be upgraded?

Help to "upgrade" this script:

$('#input-1').on('keyup',function(){
      var $this = $(this),
        val = $this.val();

      if(val.length >= 1){
      $('.label-text').addClass('label-text-active');
      }else {
      $('.label-text').removeClass('label-text-active');
      }
    });


There is a form:

<form action="">
            <div class="thumb">
              <input id="input-1" type="text" placeholder="" required autofocus />
              <label for="input-1">
                <span class="label-text">Company Name</span>
                <span class="nav-dot"></span>
              </label>
            </div>
            <div class="thumb">
              <input id="input-2" type="email" placeholder="" required autofocus />
              <label for="input-2">
                <span class="label-text">Email</span>
                <span class="nav-dot"></span>
              </label>
            </div>
                                                ....
          </form>


There are, say, 6 inputs. I check for the presence of text, and if there is text I add a class. I would like to write the script correctly so that it understands the mask which Inputs need to be checked, regardless of the id name, so as not to write 6 times -

<input id="input-*" />



$('#input-1').on('keyup',function(){
...
});

...

$('#input-6').on('keyup',function(){
...
});


and correctly write a function to check for the presence of text:

if(val.length >= 1){
  $('.label-text').addClass('label-text-active');
 }else {
  $('.label-text').removeClass('label-text-active');
}

using .toggleClass()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-01
@pawlek

$('form').on('input', 'input[id^="input-"]', function() {
  $(this)
    .closest('.thumb')
    .find('.label-text')
    .toggleClass('label-text-active', this.value.length > 1);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question