0

I have a couple of divs with the same class on one page that have a form with input fields and a submit button in them. I check with jquery for each input field if it is empty, and I want the submit button in that specific div to be disabled when theres an empty field in that div. Got this so far:

jQuery(document).ready(function(){
    $('.shipdiv input').each(function(){
    if( $(this).val() == ''){
    $(this).css("background-color","#ff0000");
    $(this).parent('.contact-button').attr('disabled', 'disabled');
}
});
   });

When theres a field empty it colors red, and then I try to disable the submitbutton that is in its parent. But thats not working. Anyone got an idea on how to do this?

HTML looks somthing like this:

<div class="shipdiv">
<form name="order_002" method="post" action="">
<input type="textfield" class="ship_data" name="bedrijfsnaam"/>
<input type="textfield" class="ship_data" name="straat" />
<input type="textfield" class="ship_data" name="postcode"/>
<input type="submit" name="ship-submit" id="ship-submit" class="contact-button" value="Aanmelden">
</form>
</div>

And then with several of these divs.

2
  • Could you post some of your HTML? Commented Jul 4, 2013 at 9:56
  • thats generated with php but ill try to recreate it. Commented Jul 4, 2013 at 9:58

2 Answers 2

2

try:

 $(this).parent('div').find('.contact-button').attr('disabled', 'disabled');

or use closest:

 $(this).closest('div').find('.contact-button').attr('disabled', 'disabled');
Sign up to request clarification or add additional context in comments.

Comments

2

It can be done by finding the parent div, relative to the current input, then finding the button within the div.

$(this).parents('.shipdiv').find('.contact-button').prop('disabled', true);

Side note: .prop('disabled', true) is preferred to .attr().

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.