Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jquery find() to select input by class

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

like image 615
JellyFishBoy Avatar asked Jul 04 '26 04:07

JellyFishBoy


1 Answers

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:

  • filter().
  • find().
like image 72
David Thomas Avatar answered Jul 06 '26 19:07

David Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!