Jump to content

[jquery] best practices question...


ahschmidt

Recommended Posts

I am slowly, but gainfully getting up to speed with using the jQuery library. I am finding that one of my favorite ways to control the state of elements on the screen is to add or remove CSS classes. Is this considered a good general tool for using jQuery? Are there any conventions, or best standards for jQuery out there? Thanks!

Link to comment
Share on other sites

My guess is that it would relate to the nature of the changes. If you are only intending to change the color, of which that is one property of many of the class, than just change that specific property. If you are wanting to completely remove all styles associated with a class, then that seems like an appropriate approach. What is the nature of the task at hand?

Link to comment
Share on other sites

One good practice off the top of my head is to only use jQuery when necessary. For example, inside a callback function, this will refer to the DOM element that triggered the event. I.e:

$('a#mylink').click(function(){	//this refers to the anchor with an ID of mylink});

To get the ID attribute of the link, i've seen people do things like $(this).attr('id'). You really don't need to do this. Sometimes you may need to wrap this inside the jQuery object to use jQuery methods, but if all you want is the id, why not just use raw JS and get it like: this.id? This saves on processing time.

Link to comment
Share on other sites

Thanks for the tips. I am still learning how best to use jQuery, and am slowly getting my head around the ins and outs. I have a navigation which reveals a secondary nav when you click a link. I am altering the display of the 2nd nav UL elements to turn them on and off. I was initially changing which CSS class was attached to the element, but now I think that altering the one CSS property makes more sense.

Link to comment
Share on other sites

Personally I think changing the class name always makes more sense than changing the value of the the specific property.It's easier to maintain, it's what CSS was designed for, and it separates your code, decoupling your view and model.

Link to comment
Share on other sites

Personally I think changing the class name always makes more sense than changing the value of the the specific property.It's easier to maintain, it's what CSS was designed for, and it separates your code, decoupling your view and model.

Link to comment
Share on other sites

Personally I think changing the class name always makes more sense than changing the value of the the specific property.It's easier to maintain, it's what CSS was designed for, and it separates your code, decoupling your view and model.
This is an interesting point.
Link to comment
Share on other sites

I see what you mean. So just toggle the classes and just modify their definitions to get the desired style output. Good call.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...