epalla.com

reddit reddit

Kristen Bell's sloth melt down on Ellen
Santorum Tells Sick Kid Not To Complain About $1 Million Drug Costs Because People Pay $900 For An iPad
The front page of Reddit on the front page of The Star Tribune. (x-post)The front page of Reddit on the front page of The Star Tribune. (x-post)
How I feel as a-
Like a boss.
"If they want to say God sent me, that’s fine. I think Reddit did it." - TheLake
1 Raptor, 1 Mustang1 Raptor, 1 Mustang
Graffiti from ScotlandGraffiti from Scotland
Pac-man dress... hotPac-man dress... hot
The captain is coming! Quick, do something important!

Updated: 02/08/12 06:00 am

Latest Comments

JamesI know, Fuck Javascript. I spent at least an hour implementing a
SatanThis is exactly what I was looking for, didn't notice the differe
Mattk.
92.625sean, all i think your's is good for is hanging clothes on. ma
MattNope. That all came to a screeching halt when I messed up my ank
seanpr123... Still hitting the stationary?
MattFor some reason the idea of Drew rocking out to this song puts a
DrewLOL! I have been guilty of listening to this song as well. Very
JoshAbout every 5 years or so I need a reminder of how amazing the Mi
JoshSo I saw this one opening night, also in 3D, and have been waitin

D
e
c
22

I came across an issue today with jQuery with multiple forms. I’ve got a page that’s dynamically generating possibly several dozen mostly identical forms on a single page. “Why would you do that?” you might ask. Well, imagine a data set where I want to be able to edit each row independently and you’ve got an idea of what I’m doing. I know it’s not pretty, but it’s definitely an easy and straight forward way to do this. Here’s an example of a typical form that I might have 15 of in this script:

1
2
3
4
5
6
7
8
9
<form action="/form/process" method="post" class="question_form">
	<label for="questiont">Question:</label>
	<input class="required" type="text" name="question" />
	<br />
	<label for="answer">Answer:</label>
	<input class="required" type="text" name="answer" />
	<br />
	<input type="submit" value="Submit!" />
</form>

So, you’d think the logical thing to do for quickly running validation on all these forms would be:

1
2
3
$(document).ready(function() {
	$(".question_form").validate();
});

Unfortunately, because the form fields shared the same name, the validation plugin hiccuped with this solution. Now I’m using custom error classes and rules in my actual implementation, so maybe this problem is more specific and isn’t reproduced quite this easily – but basically what happens is that if you have 10 “Question” forms, the validation plugin will see the “Question” field on the first form and call it good. Obviously, we can’t have that, we want each form validating separately.

The solution? Use the each selector to iterate through the forms, of course:

1
2
3
4
5
$(document).ready(function() {
	$(".question_form").each(function() {
		$(this).validate();
	});
});

voila! Easy, but I want to remember it for later :)