Rami
Rami

Reputation: 171

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

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.

Upvotes: 1

Views: 2187

Answers (1)

Angel Politis
Angel Politis

Reputation: 11313

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.

Upvotes: 5

Related Questions