The judge based his decision in part on a 1969 ruling by the United States Supreme Court that allowed students in Des Moines to wear black armbands to school in protest of the Vietnam War. He wrote that had the button depicted swastikas, a Confederate flag, or a burning cross, it would have been "plainly offensive" and he would have ruled differently.
The schools superintendent, Patricia L. McGeehan, said the district was disappointed in the ruling, and planned to review its options.
Ms. McGeehan said in a statement that the district was "very concerned with the precedent this may set not only for Bayonne but for every public school district in New Jersey that tries to create and maintain a school environment conducive to learning and that is not offensive to students or staff."
The statement added, "Images of racial and ethnic intolerance do not belong in an elementary school classroom."
The dispute over the button began last fall, when Michael DePinto, 11, who was a fifth grader at Public School 14 at the time, objected to the policy.
To protest, he and his mother, Laura, 47, made a button that included a photograph of a group of grim, identically dressed members of Hitler Youth with the words "No School Uniforms" imposed over them.
After Michael wore the button for several weeks, the district sent a letter to his home in November, demanding that he stop or face suspension. Another fifth-grade student, Anthony LaRocco, then began wearing one as well.
After the suspension threat, the boys' parents sued, claiming their First Amendment rights were being denied.
Michael said on Thursday that he had never intended to offend anyone but merely make a point about conformity.
"It's like forcing a swastika on someone," he said. "It's what Hitler did to his youth."