Passing parameters in Javascript onClick event

前端 未结 9 1051
滥情空心
滥情空心 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: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  Tag with the required parameters for Onclick call. Use double quotes.
    
    ProductButtonsHTML += "  ";
    
    }
    
    // Add the Tags to the Div's innerHTML.
    document.getElementById("divProductsMenuStrip").innerHTML = ProductButtonsHTML;
    

提交回复
热议问题