Reputation: 11297
I have inherited a piece of code which has a series of functions like the following:
$("#data[User][notify_one_day_out]").change(function () {
$("#updatenotificationsform").submit();
}
$("#data[User][notify_one_month_out]").change(function () {
$("#updatenotificationsform").submit();
}
and this goes on and on. How do I just write one function that does the same thing since every ID begins with data. Sorry am a JS newbie
Thanks
Upvotes: 0
Views: 121
Reputation: 12448
You can actually stack up those Jquery selectors would that be enough of a help or do you really have ALOT of them?
$("#data[User][notify_one_day_out], #data[User][notify_one_month_out]").change(function ()
{
$("#updatenotificationsform").submit();
}
Upvotes: 0
Reputation: 106027
In addition to @Deleteman's sound advice, remember that you can specify multiple selectors for $()
, e.g.:
$( "#data[User][notify_one_month_out]",
"#data[User][notify_one_day_out]",
"#some-other-selector",
// ...
).change(function () {
$("#updatenotificationsform").submit();
}
Upvotes: 0
Reputation: 8690
Something like this would probably work for you:
$('[id^="data"]').change(function() {
$("#updatenotificationsform").submit();
}
That basically says: grab all the elements with an Id starting with the string "data", you can read more about that here: http://api.jquery.com/attribute-starts-with-selector/
Let me know if that helps!
Edit
Alternatively if you can modify your mark up, you could assign the same class to all those elements, and then just select using the class selector.
Upvotes: 4