0

i have a simple of jquery ios button, and i want when user clicks on it and it is on, background color of page should be black and when button is off background color should be red for instance....

this is jsfiddle: https://jsfiddle.net/urbb4rgx/

what i appended to jquery:

var one = getElementsByClassName("switch"),
var two = getElementsByClassName("switchOn"),
if (two.hasOwnProperty("switchOn")) {
 document.getElementsByClassName("switchOn").background: red;
}
else {
 document.getElementsByClassName("switchOn").background: black;
}

please explain me what i did wrong

thanks...

asked Mar 18, 2016 at 20:16
2
  • Seems to work fine. jsfiddle.net/urbb4rgx/1 Commented Mar 18, 2016 at 20:23
  • yes but background color should be changed after click on the button Commented Mar 18, 2016 at 20:24

1 Answer 1

1

With jQuery try the following . --Update Everything in your code looks fine . Just modify it to the following

 $(document).ready(function(){
 $('.switch').click(function(){
 $(this).toggleClass("switchOn");
 $("body").toggleClass("black");
 });
 $('.switchBig').click(function(){
 $(this).toggleClass("switchBigOn");
 });
});

You will need to create a class to set Default background color :

body {
background-color:red; 
}
.black{
 background-color:black; 
}

Working example here: http://codepen.io/theConstructor/pen/bpgPvB

answered Mar 18, 2016 at 20:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.