How to add a onclick event to an element using javascript

前端 未结 7 1087
忘掉有多难
忘掉有多难 2020-12-04 02:03

I have created an element using document.getElementsByClassname, and would like to add a onclick event to this element, so that when someone clicks on this element onclick f

相关标签:
7条回答
  • 2020-12-04 02:05

    You have to pass reference of a function instead of adding inline alert.

    var element= document.getElementsByClassName('classname');
    function doSomething() {
      alert('clicked')
    }
    
    // add event listener to element 
    element.addEventListener("click", doSomething, false);
    
    0 讨论(0)
  • 2020-12-04 02:11

    You can use an event delegation or DOMNode.matches.

    document.addEventListener('click', function(e) {
       if (e.target.className == 'classname') // if (e.target.matches('.classname'))
       {
           //do something
       }
    });
    
    0 讨论(0)
  • 2020-12-04 02:22

    You can use .attr() jquery method for this:

    $('.classname').attr("onClick", "javascript:alert('clicked'); return false;"); 
    

    Try this

    JavaScript

    var element= document.getElementsByClassName('classname');
    element[0].onclick = function() { alert('Hello'); };//-- here i used "[0]" to refer first matched element 
    

    Try with javascript

    0 讨论(0)
  • getElementsByClassName returns an HTMLCollection, so even though you have only one element with that classname in DOM, you have to retrieve it with index 0:

    var element = document.getElementsByClassName('classname')[0];
    element.addEventListener("click", function(e) {
        alert('something');
    }, false);
    

    Alternatively, since you only have one element with the classname, you can safely use querySelector, which will return the first match element.

    var element = document.querySelector('.classname');
                                          ^
    element.addEventListener("click", function(e) {
        alert('something');
    }, false);
    

    Please note the dot in above code. querySelector accepts a CSS selector string as a parameter.

    0 讨论(0)
  • 2020-12-04 02:30
    Please try the following:
    
    var rows = 3;
    var cols = 3;
    var defaultVal=0;
    var mainDiv=document.getElementById("mainDiv")
    var arr = []
    for (var i = 0; i < rows; i++) {
        arr[i] = [];
      for(var j=0;j<cols;j++){
        arr[i][j]=defaultVal++;
        var element=document.createElement("input");
        element.type="button";
        element.className="left";
        element.addEventListener("click", testFunction, false);
        element.value=arr[i][j];
        mainDiv.appendChild(element);
    
    
      }
    
    
    }
    function testFunction(){
        alert('hi')
    }
    
    //Use element.addEventListener("click", testFunction, false); 
    //Use testFunction and not testFunction() as this "()" invokes the function
    
    0 讨论(0)
  • 2020-12-04 02:30

    I was able to add an onclick event to an element using following javascript code

    list_title = document.createElement('UL');
    list_title.onclick=function() {
    
        // place your code here
    };
    
    0 讨论(0)
提交回复
热议问题