Passing string parameter in JavaScript function

后端 未结 6 1489
夕颜
夕颜 2021-02-04 10:27

I was trying to pass a string to a JavaScript function.

As it\'s mentioned here - Pass a string parameter in an onclick function

I\'m using this simple co

6条回答
  •  温柔的废话
    2021-02-04 11:15

    Rename your variable name to myname, bacause name is a generic property of window and is not writable in the same window.

    And replace

    onclick='myfunction(\''" + name + "'\')'
    

    With

    onclick='myfunction(myname)'
    

    Working example:

    var myname = "Mathew";
    document.write('');
    function myfunction(name) {
        alert(name);
    }

提交回复
热议问题