Get which key was pressed

for the following html

    <div id="buttons">
        <button order="1" onclick="showImg()">1</button>
        <button order="2" onclick="showImg()">2</button>
        <button order="3" onclick="showImg()">3</button>
        <button order="4" onclick="showImg()">4</button>
        <button order="5" onclick="showImg()">5</button>
    </div>

how do I need js to get its order when a button is pressed?

Mar.16,2021

const order = event.target.getAttribute('order')

it is not recommended to write the event function on the html page. The js can be obtained as follows: first iterate through each button, and then use the getAttribute function to get the custom attribute value


 <div id="buttons">
        <button order="1" onclick="showImg()">1</button>
        <button order="2" onclick="showImg(this)">2</button>
        <button order="3" onclick="showImg(this)">3</button>
        <button order="4" onclick="showImg(this)">4</button>
        <button order="5" onclick="showImg(this)">5</button>
    </div>
    
    <script>
       function showImg(e){
          let order = e.target.order;    
       }
    </script>
    
Menu