views:

47

answers:

2

Hello everyone,

While developing a design using jQuery I stumbled across a problem. How would I know, without having to look through the javascript, if jQuery isn't doing a selector on a class in my HTML? That is to say, if I wanted to change the class to something else, I have no way of knowing if that class is being used elsewhere so it makes it difficult for me to easily update the design.

So, my thought was to duplicate a class name and append text (say, "-jquery") to the end of it if it is being utilized. This way, if I wanted to change the design in the future I could do so freely, and I would instantly know what elements on the page are being used in my javascript.

For example, let's say I have a simple div with a class:

<div class="header">This is a test</div>

And javascript that does something to the div:

$(".header").click(function() { 
     $(this).slideUp();
});

My idea is to change it to this:

<div class="header header-jquery">This is a test</div>

$(".header-jquery").click(function() { 
     $(this).slideUp();
});

Thereby separating my CSS and jQuery classes, making it easier to identify elements being used by jQuery, and allowing me to update the class if need be without affecting my javascript. Sure, the code looks a bit messier, but I don't mind.

The questions:

  1. Is this a common practice at all?
  2. Should I do this?
  3. Is there a better way of achieving what I want?

EDIT:

One last question:

  1. Is there, perhaps, a plugin for Visual Studio that may do this for me? That is, if it notices me editing the class of an element that is being used in jQuery, it would warn me? Or perhaps even color-codes the class name to let me know it's being used. A feature like this in the IDE would be killer...

Bara

+1  A: 

If you only want a very specific element, instead of using a class use an id. If you're looking for specific elements that are children of one particular element, put an id on that parent. I wouldn't add a class just to use as a selector for javascript.

Myles
Problem is, I'm developing in ASP.NET (non-MVC for the moment), so the ID is out of the question.
Bara
What's wrong with getting the clientId of an element?
Myles
How would I get the ClientID of an element in an external javascript file? I've seen very messy solutions to this problem, and I'd rather not have a bunch of extra code just to support this if CSS classes work fine...
Bara
Pass it in as an argument to the instantiation of the javascript object in your external file. Not an object in the external file? Pass it into the function call that needs it. Multiple functions need it? Create a function that registers ids in globally scoped variables. Seems like overloading a concept because it's convenient is asking for work down the road...
Myles
How would I pass in a clientid to a selector like the examples I gave in my original post? It isn't a function call that I'm making from my HTML, it's done entirely in the javascript. I would have to create a new variable in my js file, set that js variable in my HTML/ASP, then use that variable as my selector in my javascript. That would require a new variable for every single selector I use if I had to rely on clientid's.
Bara
Or one variable that is a data structure, that you then loop through to apply the desired function.
Myles
+3  A: 

I get what you are saying and what you want to help with. Basically, you want to want add a class to the raw HTML (not dynamically) so you know if you ever go to change it in the future, that it is in use by jQuery.

1. Is this a common practice at all?

No, or at least not that I have seen. It seems like it would only clutter your code.

Update: However, as you brought up in the comments, in an environment where your HTML coder doesn't work with jQuery, it would alert them that they could break it when they edit that class. Truth be told however, as with most jQuery code, if he touches any HTML inside that element he risks breaking the code, not just the class.

2. Should I do this?

I personally would not. If anything, you could add a rel tag since its valid on any element:

<div class="header" rel="jquery"></div>

But even that is an "incorrect use" of a the rel attribute.

3. Is there a better way of achieving what I want?

Yes! At the top of your main JavaScript file for the web project put this:

/************ Classes and Selectors In Use **************

   .header
   form .special
   #footer p

*********************************************************/

When you go to change an element, just quickly scan the top of that file. Furthermore, if you are minifying your JS for production (which you should be doing) all that will be removed so it doesn't increase file size.

The right way?

Probably the best way that does take extra work, but not all the record keeping of my suggested solution, is to do automated tests. So, if you accidentally change a class, when you run your tests they will fail where they passed before.

You can read about it here: http://stackoverflow.com/questions/96066/automated-unit-testing-with-javascript <-- I personally think the answer below the accepted answer has more to offer

Doug Neiner
That's an interesting solution. It still forces me to have to look through another file to see if something is being used (or possibly multiple js files, depending), and it forces me to have to "remember" to update the top of the js file every time I utilize a class in it or remove a class in use. Based on my experience with solutions like this, most developers won't bother to do it unfortunately... But if no one has a better solution then I'll have to use this method for the time being.
Bara
@Bara That is true, but arguably you also have to remember to *go back* to the HTML and add the `-jquery` class after you have started writing your code. My solution just keeps them all in one place. I would not spread it out across multiple JS files, it would only help if it were in one place.
Doug Neiner
That is also true, but I'm also looking it from a theoretical point of view of a designer who wants to make changes to the design. He/she would not be editing the javascript and won't think to look there, but if he sees a class named foo-jquery he'll know not to modify it (and/or be curious enough to ask the developer).
Bara
@Bara In that type of environment, I can see your solution working well. I updated my answer to include automated tests as another way of handling it as well.
Doug Neiner
Ahh, automated tests on build... That would inform me of any problems during development, but it wouldn't prevent them in the first place. It's a good suggestion though, and one I may implement regardless of whether I use any method pointed out here.
Bara