Reputation: 119
I have so many links in my webpage. About 25 links with an id="reply_x". About 20 links with id="quote_x" and some links with id="like_x". I have written different functions using JQuery for different group of links. One function will run when some one clicks on any link having reply_x id and the other will run on any link clicked having id quote_x. Now I want to make my webpage efficient. So what is the most efficient way to run these function.
I have used the following method.
$(document).ready(function(){
$('#reply_x').click(function() {
// function statements
});
});
Is it an efficient way?? If not Which is more efficient way to run the function.
Upvotes: 0
Views: 70
Reputation: 191749
First of all, IDs must be unique so there should be only one reply_x
ID-attribute element, but I'll assume that _x
is actually a number. That would get tough on your fingers having to type all of those bindings, so you should probably give all of them a class instead: reply
:
$(".reply").on('click', function () {})
This binds the event to all elements, so it may not be the most efficient. If they are all wrapped in the same container, you could use:
$("#container").on('click', '.reply', function () {})
...which seems to be more efficient as long as #container
is close to the .reply
elements based on this answer
Upvotes: 0