javascript - Stuck on adding/toggling a css class to an html element in jquery -
hey guys i'm new jquery , having ton of trouble making things on own. i'm trying change styling of css class .change, isn't working. need help. in advance.
$(document).ready(function() { $('#button').click(function() { $(this).addclass('change'); }) })
html body { margin:0; padding:0; background-color:#3366cc; } .main .container { margin-top:0; text-align:center; padding:5%; } .main h1 { color:white; font-size:100px; text-transform:uppercase; } .main h3 { color:white; font-size:50px; } .main #button { color:white; background-color:red; padding:.5%; text-align:center; width:10%; font-size:25px; margin:auto; } .change { background-color:black; }
<doctype!html> <html> <head> <title>community-help yours today</title> <link rel='stylesheet' type='text/css' href='community.css'/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type='text/javascript' src='community.js'></script> </head> <body> <div class="main"> <div class="container"> <h1>community</h1> <h3>start initiative, gain support, build project</h3> <div id="button">get started</div> </div> </div> </body> </html>
you'd best off doing this:
.button { color:white; background-color:red; padding:.5%; text-align:center; width:10%; font-size:25px; margin:auto; } .button--changed { background-color:black; }
add class css directly button.
then toggling .button-changed
class. keeps specificity down , won't encounter similar problems further down line.
by adding more specificity, such doing .main #button.changed
defer same problem further down line. imagine if need add second changed state reason?
also, don't have contain .button
within main in order use it's styles , using class can validly re-use button on page, these bonus point.
here references further explain ...
http://www.impressivewebs.com/css-specificity-irrelevant/
http://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/
Comments
Post a Comment