Solution 1 :

You need to call it with (). So, it will be onclick="ageindays()"

function ageindays() {
    var birthyear = prompt("What year were you born.. Good friend?");
}
<div class="container1">
    <h2>Challenge 1: Your Age in Days</h2>
    <div class="flexboxcontainer1">
        <div>
            <button class="btn btn-primary" onclick="ageindays()">Click Me</button>
        </div>
        <div>
            <button class="btn btn-danger">Reset</button>
        </div>
    </div>
    <div class="flexboxcontainer1">
        <div id="flexboxresult"></div>
    </div>
</div>    
<script src="project1.js"></script>

Problem :

I am really new to coding and I am following this video tutorial to make a website that displays someone’s age in days depending on the prompt input. I am trying to link an onclick attribut to an HTML button that will link to my js function. the problem is that the onclick attribute will not read the js function as a string; thus, not functioning properly. I have tried to google the solution for almost an hour now, but I have not found anything that works. Please help. EDIT: I was told to add parentheses and it still did not work.

html code:

<div class="container1">
  <h2>Challenge 1: Your Age in Days</h2>
  <div class="flexboxcontainer1">
    <div>
      <button class="btn btn-primary" onclick="ageindays()">Click Me</button>
    </div>
    <div>
      <button class="btn btn-danger">Reset</button>
    </div>
  </div>
  <div class="flexboxcontainer1">
    <div id="flexboxresult"></div>
  </div>
</div>
<script src="project1.js"></script>
</body>

</html>

js code:

function ageindays() {
  var birthyear = prompt("What year were you born.. Good friend?");
}

Comments

Comment posted by SMAKSS

You need to invoke it with parentheses.

Comment posted by Herman Thomas

when I added the parentheses, it still did not work.

Comment posted by Shane_Yo

see if you js file is loaded, maybe just add your function without any src then move it into a js file and reverence properly

Comment posted by Herman Thomas

OMG it worked!! thank you all so much!

By

Leave a Reply

Your email address will not be published. Required fields are marked *