1

I want to build a jQuery app that can have multiple instances on a single page.

However, I am running into this problem that one instance receives events from other instances. For example, if I have the following event:

$('#start_btn').click(...)

If any #start_btn on the page is clicked, then all the instances start.

So, how do I design the code so each instance only interacts with the elements within its own HTML code?

Update - If you are here with the same type of question; I found this is the wrong approach. jQuery plugin design is the best approach.

http://learn.jquery.com/plugins/

6
  • "If any #start_btn on the page is clicked" -> IDs should be unique... Commented Jul 18, 2013 at 22:03
  • I understand about the thing with the ID's. I always follow that guideline. However, I have seen other jQuery apps that work like this: You have segments of HTML with all the same id's and classes, yet the jQuery code can distinguish them from each other as independent apps. Commented Jul 18, 2013 at 22:07
  • 1
    I see your point, but other people's bad practices are not a good reason for doing the same thing... Commented Jul 18, 2013 at 22:08
  • Sure. Been down that road. But I need to make this app so there are multiple instances on the same page. And this is the only practice I've seen. Perhaps there is a better way to approach this? Commented Jul 18, 2013 at 22:11
  • 1
    I'm not sure if localize is the right term. Maybe you mean "modularize"? Commented Jul 18, 2013 at 22:26

3 Answers 3

2

You should never have two controls with the same ID on the same page. If you want absolutely separated pages, you may want to put each app in its own IFRAME, with its own full context, then they'll be totally segregated.

I suppose it depends on exactly what these things do, and how big and complex everything is.

Sign up to request clarification or add additional context in comments.

Comments

1

IDs are ment to be unique per-document. Don't use them. Instead, use a general-purpose class attribute to identify individual applications and classes (again) inside these instances too point into specific parts of the application.

Example HTML:

<div class="app">
  <a class="start-button" href="#">button1</a>
</div>
<div class="app">
  <a class="start-button" href="#">button2</a>
</div>

Assuming the above, you can now determine the application in which the start button was clicked:

$('.start-button').click(function(event) {
    var app = $(event.target).closest('.app');
    // Do stuff
});

Now if you want to do stuff inside that specific application, you can just use the app or some other top reference to keep the changes inside that app only.

For instance, hiding the button when clicked:

$(app, '.start-button').hide();

For a full running example, see this Fiddle: http://jsfiddle.net/8Xtue/2/

What I'm presenting here is a simple foundation for a JavaScript application framework. If this is the style you are writing your applications, you might want to consider some ready framework like AngularJS (http://angularjs.org/), which already provides you with a very well thought framework on working this kind of embeddable, JavaScript applications.

4 Comments

This is an interesting idea. Now, if someone clicks the button for appId 1, and something needs to happen, such as displaying a specific DIV, how would I access it the copy of that DIV in the appId1 and not appId2?
This solution works, but it is definitely over-engineered for the problem you're looking to solve. I avoid data attributes whenever I can, because they just add another layer of complexity to the HTML/JS interaction. This also makes it more difficult to re-use templated HTML, such as your start button here.
Definitely agree on the over-engineering part. Edited to remove IDs and data-attributes since they are not actually required. I archived the old version here just in case somebody wants to look it up: jsfiddle.net/8Xtue/1
I accepted this answer because, for the scenario I gave, it seems to be the most appropriate. However, in my search, I discovered I was taking the wrong approach. I read about jQuery plugin design and realized that was the correct approach. learn.jquery.com/plugins
0

You can namespace your controls.

If you post the code for your page, i can update this answer, however this is what it might look like

<div id="page1">
  <button class=".startBtn">
</div>
<div id="page2">
  <button class=".startBtn">
</div>

Then in the jquery

$("#page1 > .startBtn").click()
$("#page2 > .startBtn").click()

OR, if they are not direct parent child

$(".startBtn", "#page1").click()
$(".startBtn", "#page2").click()

Some documentation here: http://api.jquery.com/child-selector/

NOTE: I changed the startBtn to a class instead of an ID, as we generally want to avoid having duplicate IDs within the DOM.

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.