Hook into an onClick event without using the HTML property

后端 未结 3 1100
终归单人心
终归单人心 2021-01-02 17:33

I would like to keep my JavaScript and HTML code separate. To do this, I want to make sure that I never use the following syntax:



        
3条回答
  •  失恋的感觉
    2021-01-02 17:44

    By assigning an ID to your input element, you will easily (and efficiently) be able to access it with raw javascript

    
    

    In your separate javascript :

    var input = document.getElementById("myInput");
    
    input.onclick = function() {alert("clicked");}
    

    Obviously you would do something more useful than an alert in the onclick function...

提交回复
热议问题