I want to pass few parameters to setAttribute()
method
parameters are :
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 + ")");
parameter line is passed as a string, which should be passed as an array.
Thank you in advance.
I want to pass few parameters to setAttribute()
method
parameters are :
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 + ")");
parameter line is passed as a string, which should be passed as an array.
Thank you in advance.
Share Improve this question edited Feb 14, 2017 at 10:58 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Feb 14, 2017 at 10:54 Rohan NagalkarRohan Nagalkar 4432 gold badges6 silver badges15 bronze badges3 Answers
Reset to default 7You should use the addEventListener()
method instead :
newSelect.addEventListener("click", function(){
add(obj, mal_pat_id, instruction, line);
});
Hope this helps.
Why use set attribute for event listening. Do it directly using onclick
property like this:
newSelect.onclick = function() {
add(obj, mal_pat_id, instruction, line);
}
Or better like this:
newSelect.addEventListener("click", function() {
add(obj, mal_pat_id, instruction, line);
});
Note: This is not available for all kind of attributes.
You can achieve what you want by surrounding the array with "[]" this will make it seem like an array to the function
newSelect.setAttribute('onclick', "add("+ obj +","+ mal_pat_id + "," +instruction + "," +"[" +line+ "]"+ ")");
This will set all the array elements of the array line as ['a','b','c'] instead of setting them as 'a','b','c' which will make the function identify them as individual variables