N. Didier N. Didier - 29 days ago 14
PHP Question

JS function triggered by half-random id

I have many forms

<form action="#" method="post" id="my_id"> ... </form>


I have 1 form per element in my DB and i also have a JS function who send a SQL request based on the form i submit

$("#my_id").submit(function() { ... }


It does work for my 1st form only so i tried to change the id for each form like this

<form action="#" method="post" id="my_id<?php echo nb ?>"> ... </form>


where
nb
is an auto-incremented number. But the problem it doesn't enter in my function anymore (
for nb = 1, my_id != my_id1
).

How can i change my condition in the JS part? add wildcard?

$("#my_id*").submit(function() { ... }

Answer

I think this is what you are after:

$("form[id^='my_id']")

This will select all form elements with ID starting with "my_id" .