4

Well basically I have a website with styles contained within various css files, now I'd like to add a certain js, but only in one style. So I would like to be able to add it in my css file, is such thing possible?

Wingblade
  • 9,585
  • 10
  • 35
  • 48
  • 2
    Nope, no way. There used to be an Internet Explorer concoction that did something similar but it was phased out in IE8 – Pekka Jun 24 '12 at 19:15

2 Answers2

4

No, this is not possible, and nor should it be. Separate your application logic from your styling.

For when the two need to interact, you can detect certain styles with JavaScript and load various other JavaScript files from there.

Brad
  • 159,648
  • 54
  • 349
  • 530
4

No, this isn't possible. However, look into jQuery selectors to attach JavaScript functionality to certain CSS class names:

http://api.jquery.com/category/selectors/

For example, you could declare an element with a certain class name like this:

<div class="class-name"></div>

And then include some JavaScript in your document which does something to the element based on its class name, like this:

$('.class-name').click(function() {
alert('You clicked .class-name');
});

Here's a jsFiddle example of the above: http://jsfiddle.net/YAZpE/

Ian Newson
  • 7,679
  • 2
  • 47
  • 80