4

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.

3 Answers 3

7

You should use the addEventListener() method instead :

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Will this pass the "line" parameter as an array instead of string? As question is asked for this issue.
String can be simply passed using add("+ obj +","+ mal_pat_id + "," + instruction + "," + line + "). but i don't think the OP want to pass string then split it ..
Is that not what you're looking for @Rohan?
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.