1

I'm facing this issue in my code while validating AMP code

The attribute 'onclick' may not appear in tag 'button'

my code looks like

<button class="btn btn-primary float-right" role="button" onclick="calc();">

i couldn't find and answer for any related question.

Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
Rami
  • 161
  • 3
  • 18

1 Answers1

5

If AMP whines about your use of inline JavaScript, simply remove it and add it in a script or an external JavaScript file, if you happen to have one. Just be sure to give the button an id, so that you can uniquely identify it.

Code:

<script type = "application/javascript">
   document.getElementById("button-id").onclick = calc;
</script>

Notes:

  1. Remember that the main reason behind using AMP is to ensure that certain technologies don't slow down your website. Scripts, are considered one such technology and therefore, they are generally not allowed. Be sure to check out the answers on this question for more on this.

  2. As a general piece of advice, I'd recommend you separate the design from the implementation part of your web applications and avoid using inline JavaScript altogether.

Angel Politis
  • 10,955
  • 14
  • 48
  • 66
  • thanks you saved me and of course i'll take your advice in my consideration – Rami Jan 27 '18 at 14:00
  • @AngelPolitis Can you please explain how to add external JavaScript in AMP. How can we implement an existing amp project. – Bachcha Singh Jan 29 '18 at 06:59
  • Check out my first note as well as the answers in this [question](https://stackoverflow.com/q/36035733/6313073) @BachchaSingh. JavaScript is the main reason behind slow websites and let's say AMP isn't really fond of that. – Angel Politis Jan 29 '18 at 09:11