0

I'm new to javascript. My chrome extension currently is not printing text into the little window once the button is clicked.

The first bit is the popup.js and the second is popup.html. My goal is force once you click the button, the area where it says filler is replaced with the text I have set in popup.js. I don't know javascript well and this is a lot of code that I have compiled from around. Thanks everyone!

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("click-this").addEventListener("click", handler;
});

// The handler also must go in a .js file
var date = new Date();
var dayOfWeek = date.getDay();
function handler() {
  if (dayOfWeek == 0){
      return  "sunday";
  }
  else   if (dayOfWeek == 1){
        return  "monday";
    }
  else if (dayOfWeek == 2){
        return  "tuesday";
    }
  else if (dayOfWeek == 3){
        return  "wednesday";
    }
  else if (dayOfWeek == 4){
        return  "thursday";
    }
    else if (dayOfWeek == 5){
          return  "friday";
      }
      else if (dayOfWeek == 6){
            return  "saturday";
        }
       
}
<head>
  <script src="popup.js"></script>
</head>
<div class="row">
    <div class="column large-6 medium-6 small-12">
        <h1> Lunch Menu </h1>
        <p> filler filler filler</p>
        <button type="button" id="click-this">Click</button>
    </div>
</div>

<style scoped>
  div {
    height: 100px;
    width: 500px;
  }
</style>

1
  • If this question is answered please mark the appropriate answer as "answered". Welcome to SO! Commented Jan 26, 2019 at 19:22

3 Answers 3

2

It looks like you forgot to put closing brace before semicolon

document.getElementById("click-this").addEventListener("click", handler);

Hope that helps.

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

4 Comments

Thanks haha. Unfortunately nothing happened :(.
@JoeyZhao What do you want to see exactly?
@JoeyZhao to see something you need to change all these return "tuesday"; to window.alert("tuesday"); and you will see dialog box.
I want the filler text to be replaced with the text I choose, I put tuesday there right now but it will be changed later.
0

I suggest you use an editing program that has linter to check for any typing errors, it's really great when you're learning. My personal favorite is Atom, although it's a bit heavy on RAM.

2 Comments

I am using atom. It doesn't correct it for some reason though.
Did you add a javascript linter package?
-1

Added id for the paragraph to change the value as per the output.Let me know if it works!!

document.getElementById("click-this").addEventListener("click", function(){
var day;
switch (new Date().getDay()) 
{
case 0:
day = "Sunday";
break;
  case 1:
day = "Monday";
break;
  case 2:
day = "Tuesday";
break;
  case 3:
day = "Wednesday";
break;
  case 4:
day = "Thursday";
break;
  case 5:
day = "Friday";
break;
case  6:
  day = "Saturday";
}//end of switch case
document.getElementById("demo").innerHTML = day;
}//End of function
);
<head>
</head>
<div class="row">
    <div class="column large-6 medium-6 small-12">
        <h1> Lunch Menu </h1>
        <p id="demo"> filler filler filler</p>
        <button type="button" id="click-this">Click</button>
        </div>
    </div>

<style scoped>
div {
  height: 100px;
  width: 500px;

}
</style>

1 Comment

@JoeyZhao Did my code work for your requirement or not?

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.