Then the focus shifted to New Jersey, where an even more sweeping bill had been making its way through the state Legislature that would have barred nearly all exemptions to vaccines for students at any public or private school, including colleges, which were not covered by the New York law.

But on Monday the bill collapsed in spectacular fashion, torpedoed by angry parents and the mobilization of national anti-vaccine celebrities who were able to outmatch one of the state’s most powerful elected leaders.

The story of how they succeeded involves a wide range of forces in New Jersey and beyond that coalesced to doom the bill at a time when a spate of deaths from measles has been reported in Samoa, a Pacific island nation that had a low vaccination rate, and public health officials are urging greater flu vaccination because of more severe strains this year.

An influential ultra-Orthodox Jewish organization that had remained largely silent as the New York bill was being debated deliberately pivoted, opting to vocally oppose the New Jersey legislation on grounds of religious freedom.

Grassroots parent groups successfully leveraged social media and conservative talk radio in their effort to convince most Republican leaders to line up against the bill. A Facebook page named Occupy Trenton urged parents to converge at the Statehouse. And, in the final week of debate, appearances by a Kennedy scion and a contrarian filmmaker helped fuel a libertarian argument that parents, not government, should control their children’s health care.

The intense protest left two Democratic senators with cold feet that no degree of political cajoling — or a private question-and-answer session for lawmakers with three pediatricians from the state chapter of the American Academy of Pediatrics — could thaw.

“I have never seen an issue that brought together grassroots people like this,” said Sen. Robert Singer, a Republican who opposed the legislation.

He represents Lakewood, which includes a heavily Orthodox Jewish community that is home to one of the largest yeshivas in the world. But he said many of the thousands of calls and emails his office received over the past month were from non-Orthodox families.

“Many were not Republicans; many were independents and Democrats,” he said. Parents told him they feared their children would be harmed if they followed the mandatory vaccination schedule. “I saw people call me, scared,” Singer said.

Doctors and public health experts have said the legislation was needed to halt the uptick in the number of unvaccinated children in New Jersey, and to prevent the kind of measles outbreak that occurred in the region last year. They emphasized that there was an overwhelming scientific consensus that vaccines are safe and effective.

The bill passed last month in the Assembly. But lawmakers who supported the legislation also may have made a political miscalculation when they introduced an amendment that excluded private schools to win the vote of a Republican needed to achieve a majority in the Senate. Instead, opponents, including an African American Democratic assemblyman, argued that this amounted to segregation that would allow only the affluent a choice about vaccination.

Both sides have described the clash in New Jersey as a key front in a nationwide conflict, and perhaps the biggest victory for vaccine skeptics seeking to counter a growing effort to end religious exemptions to childhood vaccines.

“We’re ready to go to war on this,” the powerful Senate president, Stephen M. Sweeney, said after it was clear the bill did not have the votes it needed to pass on the final day of New Jersey’s two-year legislative session. A new bill was introduced Tuesday, and Sweeney, a Democrat, has vowed that it will eventually pass. The state’s Democratic governor, Philip D. Murphy, had not taken a public stance on the bill, a factor that Sweeney said was not helpful during the negotiations.

On Wednesday, Daniel Bryan, a senior adviser to the governor, said Murphy had “made his position on the importance of vaccinations crystal clear.” The governor, he added, was “disappointed that legislation supporting that goal didn’t reach his desk, but he remains optimistic that it will in the future.

Beside New York, a small group of other states, including California, Mississippi and West Virginia, have ended religious and philosophical exemptions to vaccination, and there is legislation pending to do so in Connecticut and Massachusetts. Maine lawmakers also approved ending most nonmedical exemptions last year, but, under heavy pressure from groups opposed to mandatory vaccines, will ask voters to decide the fate of the policy in a March ballot referendum.

“New Jersey is the state that is arguably the home of the pharmaceutical industry, and we just won in their backyard,” said Del Bigtree, an anti-vaccine activist who lives in Texas and flew in to lead a daylong protest Monday in Trenton.

It was the second appearance within a week by Bigtree, who produced a documentary, “Vaxxed: From Cover-Up to Catastrophe,” and hosts an online anti-vaccination show. Robert F. Kennedy Jr., who campaigns against vaccines as a director of the Children’s Health Defense network, also appeared last week at a rally in the state capital.

Their star appeal became instant fodder for Facebook groups that many protesters cited as key organizing tools.

“Technology is a huge piece of it,” said Sue Collins, a founder of the New Jersey Coalition for Vaccine Choice. “Everybody has access to everybody, and they’re holding it in their hands all day long.”

(BEGIN OPTIONAL TRIM.)

The omnipresence of social media also gave opponents the ability to reach directly into lawmakers’ private lives.

Sen. Richard J. Codey, a Democrat and a former New Jersey governor, said his son got calls at home. Francine Weinberg, a daughter of one of the bill’s sponsors who lives in California, said she had to adjust her Facebook privacy settings to end the attacks from commenters.

“I call it the politics of harassment,” said Weinberg, whose mother, Sen. Loretta Weinberg, was a primary sponsor of the legislation.

“And that’s really what it felt like,” Weinberg added.

(END OPTIONAL TRIM.)

Among the radio personalities who opposed the bill was Bill Spadea, a Republican who supports President Donald Trump and hosts a morning show on one of New Jersey’s largest radio stations.

“That’s what it looks like when New Jerseyans fight back against government intrusion into our families,” he wrote on Twitter, sharing a video of protesters at the Statehouse on Monday.

Avi Schnall, New Jersey’s director of Agudath Israel of America, a nationwide umbrella organization of ultra-Orthodox Jews, said the group had decided to publicly oppose the New Jersey legislation after regretting it had not done more to stop the measure in New York.

“We learned from our mistake,” he said in an interview last month.

Last spring, the organization had quietly opposed the New York bill, but the context had been different: The debate was taking place during an outbreak centered in the Orthodox community.

As a group, Orthodox Jews, most of whom do vaccinate their children, did not want to appear opposed to immunization. But the underlying principle of religious accommodation, the organization finally decided, was one worth fighting for, in part because there are rare cases in which a rabbi might decide a vaccination was unwarranted.

“The experience in New York, which we regarded as a loss, galvanized us into appreciating more the importance of speaking publicly,” said Rabbi David Zwiebel, the executive vice president of Agudath Israel of America.

Over the past five years, the national movement of groups opposed to mandatory vaccination has surged in strength and size. It has adherents among the far right and the far left, who resist easy categorization.

“We’ve got a lot of families to be boots on the ground,” said Jackie Schlegel, a founder of Texans for Vaccine Choice, which, she says, has more than 10,000 members. “We’re much bigger than just social media warriors. ”

They are fueled and reinforced by hundreds of websites that promote anti-vaccination rhetoric and go largely unchecked.

Caught flat-footed, the mainstream scientific community has not yet mobilized a robust response.

“The anti-vaxx movement in America has gained strength because it runs largely unopposed,” said Dr. Peter Hotez, a pediatrician and researcher who is dean of the National School of Tropical Medicine at Baylor College of Medicine. “There is no big national counter to its rhetoric.”

Hotez, the author of “Vaccines Did Not Cause Rachel’s Autism,” about his daughter’s condition, also points the finger at a “rise, generally, of the anti-science movement over the last few years.”

The number of children with religious exemptions in New Jersey has been rising. In the 2013-14 school year, about 9,000 New Jersey school children, or 1.7%, had religious exemptions, and the statewide vaccination rate was 95.3%, according to a survey by the state’s Department of Health. In the 2018-19 school year, 94.2% of New Jersey school children were fully vaccinated, and about 14,000 children, or 2.6%, had religious exemptions.

(STORY CAN END HERE. OPTIONAL MATERIAL FOLLOWS.)

Dr. Alan Weller was one of the doctors who met behind closed doors with the Democratic senators in an effort by Sweeney to reach a 21-vote majority.

Weller said it was essential for healthy children to be vaccinated in order for children with valid medical exemptions, who are too sick to be immunized, to remain safe.

“Medical-only exemptions provide a cocoon for these kids,” said Weller, the associate director of pediatric medicine at Rutgers Robert Wood Johnson Medical School and the president of the state chapter of the American Academy of Pediatrics.

“Children in school need to be protected,” he said.

This article originally appeared in The New York Times .