For setting click events on multiple elements on a web page, is there any problem with doing the following?
-
What are people's opinions on the following method for handling a large number of clickable elements on a web page? First, here is the HTML for four elements, each with the commonButton class, and unique IDs: <div class="commonButton" id="commonButton1">Up</div> <div class="commonButton" id="commonButton2">Down</div> <div class="commonButton" id="commonButton3">Left</div> <div class="commonButton" id="commonButton4">Right</div> Then some jQuery+JavaScript to bind the same click function to each commonButton element: $(".commonButton").bind("click", function() { switch (this.id) { case "commonButton1": doSomething(); break; case "commonButton2": $("#something").hide(); break; case "commonButton3": goLeft(); break; case "commonButton4": submitForm; break; default: break; } }); When any of the commonButton elements are clicked, the common function is called, with a switch statement deciding what action to take (based on the element ID which is passed with the event). On the one hand, this method is very easy to maintain in the code and adding a new button becomes simplicity itself, and it's a lot less code than adding a new bind to each individual button. On the other hand, I currently have 61 buttons all with the same bind using the above method! It all works very smoothly and has greatly reduced my code all round, but it just seems too easy. There has to be a catch, right?
-
Answer:
for a large number of clickable elements you can use delegate. jQuery code: $(function(){ $("body").delegate('div.commonButton', 'click', function(e){ if ($(e.target).is('#commonButton1')) { alert('button1 clicked'); } else if ($(e.target).is('#commonButton2')) { alert('button2 clicked'); } }); }); also you can replace body by parent node
Loki You at Quora Visit the source
Related Q & A:
- How to call .aspx page from a web web service(service.svc?Best solution by Stack Overflow
- How to check if a web page loads?Best solution by Server Fault
- In the simplest clearest language possible, can any one tell me step by step how to get a web page or domain?Best solution by Yahoo! Answers
- What are the key elements in a facebook page?Best solution by facebook.com
- How do I set up a web page?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.