This tutorial shows you how to change the CSS file included into your page, using jQuery.
For example, if we have 2 CSS files, "style.css" and "style2.css", we can set a jQuery code to alternate the CSS included into our page, between these two files, when the user clicks on a specific element.
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>Change CSS file</title> <link href="style.css" rel="stylesheet" type="text/css" id="stl" /> <script type="text/javascript" src="jquery_library.js"></script> <script type="text/javascript"><!-- // the CSS file is chosen according to the positive or negative value of this variable var plusmin = 1; $(document).ready(function() { // when click on the element with id="idd" $('#idd').click(function(){ plusmin *= -1; // change the polarity of "plusmin" var var stl = plusmin>0 ? 'style.css' : 'style2.css'; // sets the css file, according to plusmin // change the css file of the tag with id="stl" and rel="stylesheet" $('#stl[rel=stylesheet]').attr('href', stl); }); }); --></script> </head> <body> <div id="idd"> Click here:<br /> Alternate the CSS file for this page<br /> Between "style.css" and "style2.css"</div> <p class="hdp">This paragraph has display:none; in style2.css</p> </body> </html>
body { text-align: center; } #idd { width: 200px; background: #a7efa8; margin: 2px auto; border: 2px solid blue; padding: 5px 8px; text-align: left; font-size: 16px; cursor: pointer; }
#idd { width: 300px; margin: 2px auto 2px 80px; background: #dadafe; border: 2px dashed #a0cea1; padding: 5px 8px; font-size: 16px; font-weight: bold; text-align: center; cursor: pointer; } p.hdp { display: none; }
This paragraph has display:none; in style2.css
<div>Web Programming and Development</div>
h2 { text-decoration: underline; }
function someFunction() { alert("CoursesWeb.net"); } setTimeout("someFunction()", 2000);
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea")); $nr_food = count($food, 1); echo $nr_food; // 6