1

I want to change the color of one div by clicking "1" key. I have a bunch of videos playing and when you click "1", a certain video starts playing. I want to create something that looks like a control panel, so that when you click "1" the video will turn on (which I have working with keypress in javascript) but will also change the color of my div when I press "1"

HTML

</head>
<body>

<div class="intro">Press The Number Keys and Q W E To Activate Video
</div>

<div id="one">1</div>

JAVASCIPT JQUERY

$(document).ready(function() {
  $(document).keypress(function(event) {
    if (event.which === 49) {
      $("#one").css('background-color'),("#00ffff");
    }
  });
 });


document.onkeypress = function(e) {
  console.log(e)
  if (e.key === "1" ) {
   var vid = document.getElementById('vid');
   vid.paused ? vid.play() : vid.pause();
   var vid = document.getElementById('vid').style.opacity = '1';

  } else if (e.key === "2" ) {
   var cami = document.getElementById('cami');
   cami.paused ? cami.play() : cami.pause(); 
   var cami = document.getElementById('cami').style.opacity = '1';

  }

2 Answers 2

1

Something like this should get you there. Just sandboxed the background color change logic.

<div class="intro">Press The Number Keys and Q W E To Activate Video
</div>


<div id="changeExample">This is a test</div>

document.onkeypress = function(e) {
  console.log(e)
  if (e.key === "1" ) {
        if( $("#changeExample").hasClass("red")  ){

            $("#changeExample").removeClass("red").addClass("pink").css("background-color", "pink");

        }
        else if( $("#changeExample").hasClass("pink") ){
            $("#changeExample").removeClass("pink").addClass("red").css("background-color", "red");
        }
        else{
            $("#changeExample").addClass("red").css("background-color", "red");
        }

  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much i got this to work!! Also do you know how I could change it back to pink by clicking 1 again?
Accept my answer and I will start whipping something up for you for sure.
0

Simply add this to your if statement: vid.style.backgroundColor = "yourcolor";

1 Comment

if (event.which === 49) id.style.backgroundColor = "#00ffff";

Your Answer

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.