Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:08:29+00:00 2026-05-26T16:08:29+00:00

New on HTML5 there’s an invalid event, to which you can add a listener:

  • 0
  1. New on HTML5 there’s an “invalid” event, to which you can add a listener:

    document.addEventListener('invalid', function(e){
       var element = $(e.target);
       element.addClass("invalid");
       element.parent().addClass("invalid");
    }, true);
    

    Please note, this event just works when submitting the form… If I style the input input:invalid { background: red }, the style is applied when the user starts typing and his input is not valid. Is that event only fired on submit? I tried adding the listener to the inputs themselves instead of the document and it didn’t work.

  2. I add a listener in order to apply a style to the input’s parent… Now, when the user corrects it, it’s valid again… I know there’s not a “valid” event, so, how can I accomplish it?


Ok, so here’s a fiddle –> http://jsfiddle.net/Osoascam/ceArQ/7/
The invalid listener seems to be only fired on submit… I just wanted to know whether there’s a way to add a handler just like there is for focus. See that if you type a

Thanks in advance,

Óscar

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:08:30+00:00Added an answer on May 26, 2026 at 4:08 pm

    You should use the :invalid pseudo selector and the input or the change event, to solve your problem.

    $(document).bind('change', function(e){
        if( $(e.target).is(':invalid') ){
            $(e.target).parent().addClass('invalid');
        } else {
            $(e.target).parent().removeClass('invalid');
        }
    });
    

    Here is a simple fiddle http://jsfiddle.net/trixta/YndYx/.

    If you want to remove the error class as soon as possible you should add the error class on change and remove it on the input event (Note: input event is much better than here suggested keyup, simply because it also is triggered on paste etc., but it only works with input elements, not textarea.)

    And here is a fiddle using a mixture of input and change event:
    http://jsfiddle.net/trixta/jkQEX/

    And if you want to have this cross browser you can simply use webshims lib to polyfill. Here is a x-browser example:
    http://jsfiddle.net/trixta/RN8PA/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HTML5 has a new global attribute, hidden , which can be used to hide
There is a new HTML5 attribute for input field required which pops up a
In HTML5 there is a new input type, 'search'. On most browser it's just
I want to create a new element with html5 data attributes defined, however they
I am pretty new to HTML5. Coming from Flash background, I can call the
First of all, I'm fairly new to jQuery and HTMl5/CSS3, so while there is
I know the html5 audio stuff is all very new, but is there a
In HTML5 there is a new tag <input type=email> when I am using it
I was checking out HTML5 new javascript commands and there is something similar to:
I am new to HTML5 and I am exploring HTML5 features. There I came

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.