I want to create one(or two) button(s) to switch my theme from dark to bright, i didn’t know anything about jquery but I read something in w3schools and other references so I wrote a Jquery function that change some properties in my css file:
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").css({
"color": "white",
});
$("div").css({
"background-color" : "black",});
});
});
</script>
I put this script in bottom of header tag in header.php file, and I add a button to the footer of site
<button class="bright">Ø·Ø±Ø Ø±ÙØ´Ù</button>
but when I click on the button nothings happen, when I try this code on w3shcools editor it works but the button in my wordpress site does nothing.
here is my code in W3shcools editor:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").css({
"color": "white",
"background-color": "#98bf21",
"font-family": "Arial",
"font-size": "20px",
"padding": "5px"
});
});
});
</script>
</head>
<body>
<button>Set multiple CSS properties for all p elements</button>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
And if there is a way to call that function with “href” I will grateful to help me how can I call it.
You have a syntax error, try this,
You had a , after
"background-color" : "black"
and"color": "white"
And yeah, do add jQuery library, if you haven’t like this,
You need to include the jQuery library on your page. You are using jQuery correctly but jQuery was not included on your page. Add the script like this.
The nicest way to do this is
addClass()
:Put your css into a class in the css file:
Then use
addClass()
to add the css class to the element in JQuery:This keeps your style separate from your JQuery which is a nicer way to work as it makes the style re-usable and makes a better separation between functionality (JQuery and JavaSCript) and style (css). You don’t have to go to two places to change a colour now and if you want to add the styles elsewhere you can do so simply by using the class.
It also makes for cleaner, more understandable JQuery.
You can also use
removeCLass()
to do the opposite.