How to pass an array to setAttribute method in javascript

I want to pass a few parameters to the method setAttribute() parameters:

var obj = string/this;
var mal_pat_id = "avx";
instruction = "some_instruction";
line = ['a','b','c'];

var newSelect = document.getElementById("dialog_ok_btn_for_mal_pat_conf_yes");

newSelect.setAttribute('onclick', "add("+ obj +","+ mal_pat_id + "," +    instruction + "," + line + ")");

the string parameter is passed as a string, which should be passed as an array.

Thanks in advance.

+4
source share
2 answers

Instead, you should use : addEventListener()

newSelect.addEventListener("click", function(){
    add(obj, mal_pat_id, instruction, line);
});

Hope this helps.

+7
source

Why use the set attribute to listen for events. Do this directly using the property onclickas follows:

newSelect.onclick = function() {
      add(obj, mal_pat_id, instruction, line);
}

Or better:

newSelect.addEventListener("click", function() {
      add(obj, mal_pat_id, instruction, line);
});

Note. This is not available for all attribute types.

+1
source

Source: https://habr.com/ru/post/1669686/


All Articles