Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Gard Mikael Fjeldavli
19,416 PointsPage with many forms
I'm trying to create my own webpage, where I have several blog posts and each post has its own comment field. Meaning I have a form element for each blogpost. The form element looks something like this:
<form name="feedback" action="#" method="post">
<input type="hidden" name="post_id" value="<?php echo $post_id ; ?>">
<label for="name">NAVN</label>
<input type="text" class="name" name="name">
<label for="comment">KOMMENTAR</label>
<textarea type="text" class="comment" name="comment"></textarea>
<input type="submit" value="Submit">
</form>
Now, in JavaScript I want to write something which disables the submit button if the inputfields (for the given form) does not fulfill certain requirements (lets say length). I've tried many things, like ie:
$(':input').keyup(function(){
if($('.name').val().length > 3 ){
if( $('.comment').val().length > 10){
$(this).siblings('input[type="submit"]').attr('disabled', false);
}
}
});
I understand why the code above dont work, since the $('.name) selector targets all the elements with the '.name' class name. But how do I make it only select the name and comment fields of the form that is being used.
4 Answers
Zoltán Holik
3,401 PointsNot the best solution, but it works :)
<form class="feedbackForm" name="feedback" action="#" method="post">
<label for="name">NAVN</label>
<input type="text" class="name" name="name">
<label for="comment">KOMMENTAR</label>
<textarea type="text" class="comment" name="comment"></textarea>
<input type="submit" value="Submit" disabled="disabled">
</form>
$('.feedbackForm').on('input', function(e){
if($(this).find('.name').val().length > 3 && $(this).find('.comment').val().length > 10){
$(this).find('input[type="submit"]').attr('disabled', false);
} else {
$(this).find('input[type="submit"]').attr('disabled', true);
}
});
Gard Mikael Fjeldavli
19,416 PointsThanks for the answer, but this does enable ALL the submit buttons on the page.
Zoltán Holik
3,401 PointsI don't think so :) This only enable that submit button which form you are using...
Try it please: https://jsfiddle.net/86b0s9dr/
Gard Mikael Fjeldavli
19,416 PointsYour absolutely right! I messed up with my own code! Awesome! Thank you! =)
Ryan Field
Courses Plus Student 21,242 PointsOh, please disregard my previous reply. It's really early here and my brain wasn't quite braining yet. Glad to hear you found an answer! :)