Passing parameters in Javascript onClick event

前端 未结 9 1011
滥情空心
滥情空心 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: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;
    }
    

提交回复
热议问题