Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include javascript via css? [duplicate]

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?

like image 485
Wingblade Avatar asked Oct 16 '25 13:10

Wingblade


2 Answers

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.

like image 114
Brad Avatar answered Oct 19 '25 03:10

Brad


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/

like image 22
Ian Newson Avatar answered Oct 19 '25 02:10

Ian Newson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!