Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit .css file from html via jquery

Tags:

html

jquery

css

Is it possible to change attribute properties in a style.css file using a jquery onclick event from html file?

I am using the below currently, but I would like to change the css file itself.

$('#demo').click(function(){
    $(.#demo').slideUp();
     this.style.backgroundColor = 'red';
});
like image 246
Rhys Avatar asked Jun 09 '26 06:06

Rhys


1 Answers

You cannot edit the file itself. However, you can override styles a number of ways.

  1. Create a new style element with the required rules and append to the document. (Example)
  2. Select elements and apply .css to change the desired properties. (Example)
  3. Define CSS selectors (possibly in concert with #1 above), and add or remove them from items using .addClass and .removeClass. (Example)

For the type of behavior you describe, you'd use #2 or #3 (#3 is recommended because it's the easiest to maintain). Here's #2 in practice using your example: http://jsfiddle.net/HackedByChinese/HTNGs/1/

$('#demo').click(function() {
    $(this).css('background-color', 'red');
});​
like image 102
moribvndvs Avatar answered Jun 11 '26 20:06

moribvndvs



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!