Passing parameters in Javascript onClick event

前端 未结 9 972
滥情空心
滥情空心 2020-12-02 09:43

I\'m trying to pass a parameter in the onclick event. Below is a sample code:

相关标签:
9条回答
  • 2020-12-02 10:14

    or you could use this line:

     link.setAttribute('onClick', 'onClickLink('+i+')');
    

    instead of this one:

    link.onclick=  function() { onClickLink(i+'');};
    
    0 讨论(0)
  • 2020-12-02 10:18

    onclick vs addEventListener. A matter of preference perhaps (where IE>9).

    // Using closures
    function onClickLink(e, index) {   
        alert(index);
        return false;
    }
    
    var div = document.getElementById('div');
    
    for (var i = 0; i < 10; i++) {
        var link = document.createElement('a');
    
        link.setAttribute('href', '#');
        link.innerHTML = i + '';
        link.addEventListener('click', (function(e) {
            var index = i;
            return function(e) {
                return onClickLink(e, index);
            }
        })(), false);
        div.appendChild(link);
        div.appendChild(document.createElement('BR'));
    }
    

    How abut just using a plain data-* attribute, not as cool as a closure, but..

    function onClickLink(e) {       
        alert(e.target.getAttribute('data-index'));
        return false;
    }
    
    var div = document.getElementById('div');
    
    for (var i = 0; i < 10; i++) {
        var link = document.createElement('a');
    
        link.setAttribute('href', '#');
        link.setAttribute('data-index', i);
        link.innerHTML = i + ' Hello';        
        link.addEventListener('click', onClickLink, false);
        div.appendChild(link);
        div.appendChild(document.createElement('BR'));
    }
    
    0 讨论(0)
  • 2020-12-02 10:22

    This will work from JS without coupling to HTML:

    document.getElementById("click-button").onclick = onClickFunction;
    
    function onClickFunction()
    {
        return functionWithArguments('You clicked the button!');
    }
    
    function functionWithArguments(text) {
        document.getElementById("some-div").innerText = text;
    }
    
    0 讨论(0)
  • 2020-12-02 10:25

    This is happening because they're all referencing the same i variable, which is changing every loop, and left as 10 at the end of the loop. You can resolve it using a closure like this:

    link.onclick = function(j) { return function() { onClickLink(j+''); }; }(i);
    

    You can give it a try here

    Or, make this be the link you clicked in that handler, like this:

    link.onclick = function(j) { return function() { onClickLink.call(this, j); }; }(i);
    

    You can try that version here

    0 讨论(0)
  • 2020-12-02 10:29

    This happens because the i propagates up the scope once the function is invoked. You can avoid this issue using a closure.

    for (var i = 0; i < 10; i++) {
       var link = document.createElement('a');
       link.setAttribute('href', '#');
       link.innerHTML = i + '';
       link.onclick = (function() {
          var currentI = i;
          return function() { 
              onClickLink(currentI + '');
          }
       })();
       div.appendChild(link);
       div.appendChild(document.createElement('BR'));
    }
    

    Or if you want more concise syntax, I suggest you use Nick Craver's solution.

    0 讨论(0)
  • 2020-12-02 10:32

    Another simple way ( might not be the best practice) but works like charm. Build the HTML tag of your element(hyperLink or Button) dynamically with javascript, and can pass multiple parameters as well.

    // variable to hold the HTML Tags 
    var ProductButtonsHTML  ="";
    
    //Run your loop
    for (var i = 0; i < ProductsJson.length; i++){
    // Build the <input> Tag with the required parameters for Onclick call. Use double quotes.
    
    ProductButtonsHTML += " <input type='button' value='" + ProductsJson[i].DisplayName + "'  
    onclick = \"BuildCartById('" + ProductsJson[i].SKU+ "'," + ProductsJson[i].Id + ")\"></input> ";
    
    }
    
    // Add the Tags to the Div's innerHTML.
    document.getElementById("divProductsMenuStrip").innerHTML = ProductButtonsHTML;
    
    0 讨论(0)
提交回复
热议问题