0

I have a form with one input field for the emailaddress. Now I want to add a class to the <form> when the input has value, but I can't figure out how to do that.

I'm using this code to add a class to the label when the input has value, but I can't make it work for the also:

function checkForInputFooter(element) {
    
    const $label = $(element).siblings('.raven-field-label');
  
        if ($(element).val().length > 0) {
            $label.addClass('input-has-value');
        } else {
            $label.removeClass('input-has-value');
        }
    }
  
// The lines below are executed on page load
$('input.raven-field').each(function() {
    checkForInputFooter(this);
});

// The lines below (inside) are executed on change & keyup
$('input.raven-field').on('change keyup', function() {
    checkForInputFooter(this);  
});

Pen: https://codepen.io/mdia/pen/gOrOWMN

4
  • could you please create a codepen for this ? Commented Aug 5, 2020 at 8:08
  • @TechnoTech Pen is added to the question. Commented Aug 5, 2020 at 8:18
  • your code is working fine for the Label, did you mean you want to add the same class to the Form? Commented Aug 5, 2020 at 8:29
  • @HamzaDahmoun Yes, that's what I need, add the class also the the <form> element. Commented Aug 5, 2020 at 8:38

3 Answers 3

1

This is the solution using jQuery:

function checkForInputFooter(element) {
    // element is passed to the function ^
        
    const $label = $(element).siblings('.raven-field-label');      
    var $element = $(element);
    if ($element.val().length > 0) {
        $label.addClass('input-has-value');
        $element.closest('form').addClass('input-has-value');
    } else {
        $label.removeClass('input-has-value');
        $element.closest('form').removeClass('input-has-value');
    }
}
      
// The lines below are executed on page load
$('input.raven-field').each(function() {
    checkForInputFooter(this);
});
    
// The lines below (inside) are executed on change & keyup
$('input.raven-field').on('change keyup', function() {
    checkForInputFooter(this);  
});

I've updated your pen here.

Sign up to request clarification or add additional context in comments.

1 Comment

That's awesome! Thanks!
1

Here it is, using javascript vanilla. I selected the label tag ad form tag and added/removed the class accoring to the element value, but first you should add id="myForm" to your form html tag. Good luck.

function checkForInputFooter(element) {
    // element is passed to the function ^
    let label = element.parentNode.querySelector('.raven-field-label');
    let myForm = document.getElementById("myform");
    let inputValue = element.value;
    if(inputValue != "" && inputValue != null){          
      label.classList.add('input-has-value');
      myForm.classList.add('input-has-value');
    }
    else{
      label.classList.remove('input-has-value');
      myForm.classList.remove('input-has-value');
    }
    }

Comments

1

You can listen to the 'input' event of the input element and use .closest(<selector>) to add or remove the class

$('input').on('input', function () {
  if (!this.value) {
    $(this).closest('form').removeClass('has-value');
  } else {
    $(this).closest('form').addClass('has-value');
  }
})

Edit: https://codepen.io/KlumperN/pen/xxVxdzy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.