0

I have a unknown number of different ID's which looks like #send_1, #send_2 and so on. I need to call a jQuery for each of that ID like:

$(document).on("submit", "#send_1", function(e){
    var i=1;
    if (something is true) {
        do something
    }    
});
$(document).on("submit", "#send_2", function(e){
    var i=1;
    if (something is true) ) {
        do something
    }    
});

So i could write a hundred times the same function just changing _x but there shall be a more propper way to solve this.

I usually dont use jQuery so if there is someone who could help me out I really would appreciate.

Thanks in advance.

1
  • my answer had a typo. Try it now Commented Jul 21, 2015 at 20:45

2 Answers 2

3

Use the attribute starts with(^=) selector

$(document).on("submit", '[id^="send_"]', function(e){
    var i=1;
    if (something is true) ) {
        do something
    }    
});
Sign up to request clarification or add additional context in comments.

Comments

1

you can also separate them with a ','

like so:

$(document).on("submit", "#send_1, #send_2", function(e){
//...
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.