views:

39

answers:

2

Im using multiple ASCX controls on one page, and the javascript clashes obviously if I use two of the same control. So Ive changed it all to proper OOP javascript so they dont interfere, but my problem now is how do I do the HTML side of things. Both ASCX's will make a div called "foo". So whats the usual way around this? Am I meant to also be generating all the html from inside my JS classes?

Thanks :)

+1  A: 

Put them into their own ''containers'': divs with unique IDs such as:

...
<div id="control1">
...
<!-- Control One goes here -->
<div class="foo">...</div>
...
</div>
...

<div id="control2">
...
<!-- Control Two goes here -->
<div class="foo">...</div>
...
</div>
...

You can then manipulate them by first navigating to the uniquely named DIVs, then searching for elements with class foo.

To access these in Javascript you could use something like:

var c1 = document.getElementById("control1").getElementsByClassName('foo')[0];
var c2 = document.getElementById("control2").getElementsByClassName('foo')[0];

Make sure to use the [0] on each, because getElementsByClassName returns an array of elements. You (probably) need only one element (not an array object).

mkoistinen
Is there a function like parentObj.getChildOf(foo's id) ? Or how would it work?
Matt
Dang, I can't really post code snippits here in the window. Basically, you need to use the DOM traversal methods in Javascript. I LOVE using jQuery (jQuery.org) for this, however, so: $("#control1 .foo") would return the div with class "foo" which is a child of the div with ID "control1".
mkoistinen
Oh ok, great! In this case its easier for me to implement M4N's answer, but yours will be a great help in future, thanks.
Matt
+1  A: 

You can add runat="server" to your DIVs to make them server-side controls:

<div id="foo" runat="server"> ... </div>

then use the control's (generated) ClientID:

<script>
  var divId = "<%= foo.ClientID %>";
</script>
M4N
Sweet, works well :). One more thing, each control has "var thisObj = new myObject();" which obviously dies too. Is there a way to do a similar thing with that? I don't have direct control of the page with the controls, only the controls themselves. Thanks!
Matt
@Matt: I guess this depends on what exactly you're trying to do. One possible solution (based on jquery) might be something like this: `$("#<%= foo.ClientID %>").slideOut();` (if you want to call a jquery metho) or `someMethod($("#<%= foo.ClientID %>"))` alternatively.
M4N
A better explanation: Each control will initialize the function(class) with all its code on pageload. But if both pages say var thisObj = new myObject(); then when one page calls thisObj then there are two of those. In your above example it looks like you have attached callSomeMethod(); to a DOM object? If so, could I have $("#<%= foo.ClientID %>").myObject.callSomeMethod(); or something like that. Hope im making sense :-/
Matt
@Matt: sorry, I noticed my error in the comment, and it seems I corrected it at the same time when you wrote your comment.
M4N