Using jquery find() to select input by class

29,580

To find an element based on its class, and while excluding button elements:

$(selector).find('input.className:not("button")');

Or, a more verbose manner:

$(selector).find('input:not("button")').filter(
     function(){
         return $(this).hasClass('className')
     });

References:

Share:
29,580
JellyFishBoy
Author by

JellyFishBoy

I was born and raised in the renowned city of Bristol. Since growing up within the industry of my father’s Graphic Design company, I have developed an unhealthy obsession to all things related to technology and the web. I decided to channel this passion by learning UI design and web development. Refining my skills over the course of six years, I have flourished into an ambitious business-driven individual. I have spent the past few years freelancing and contracting for some exciting companies including Dyson, Blak Pearl and Future PLC.

Updated on July 09, 2022

Comments

  • JellyFishBoy
    JellyFishBoy almost 2 years

    I have a script which validates each stage of a form and adds a tick or cross to each section if the validation is without errors or not.

    Here is my code:

    var error = 1;
    var hasError = false;
    $('.edit_artist').children(':nth-child(' + parseInt(step) + ')').find('input:not(button)').each(function() {
                    var $this = $(this);
                    var valueLength = jQuery.trim($this.val()).length;
                    if(valueLength == '') {
                        hasError = true;    
                    }
    

    I dont want the validation to occur on all input fields, so I want to be able to validate inputs by class. Is there a way of finding inputs by class or would I need to go down another route to achieve this?

    Thanks