0

I am trying to call a javascript function that contains dynamic variable as a parameter. I think i am failing in the syntax to get this particular function executed. I have tried a few combinations and none of them seem to work. Please can someone advise..

 for( i=0; i<succeedList.length; i++){
     var file_uniq_id = succeedList[i].filename_uniq;

 //Creating dynamic button
 var subm_btn = document.createElement("INPUT");
 subm_btn.setAttribute("onclick", "UploadMyScript.submitTitle(this.id,'+file_uniq_id+')");
 p_titleBtn.appendChild(subm_btn);
}



 UploadMyScript.submitTitle = function(id, uniqID){
 // Does something ....
 }

My problem is I cannot appear to pass on the 'file_uniq_id' value to UploadMyScript.submitTitle().

3
  • 1
    subm_btn.setAttribute("onclick", "UploadMyScript.submitTitle(this.id,'+file_uniq_id+')"); contains a string as the second parameter. You probably want subm_btn.setAttribute("onclick", UploadMyScript.submitTitle(this.id,'+file_uniq_id+')); Commented May 31, 2015 at 17:56
  • 1
    because you are not concatenating correctly because you used the wrong quote to end the string, it should be: "+file_uniq_id+" Commented May 31, 2015 at 17:58
  • What does I cannot appear to pass on the 'file_uniq_id' value to UploadMyScript.submitTitle(). mean? What happens when you try? Do you get an error? What is the error? What line causes it? Commented May 31, 2015 at 17:58

1 Answer 1

4

Why not use an event listener ?

subm_btn.addEventListener('click', function () { 
    UploadMyScript.submitTitle(subm_btn.id, file_uniq_id); },
false);
Sign up to request clarification or add additional context in comments.

3 Comments

When i use this event listener. The error that i am getting back is 'Uncaught ReferenceError: file_unique_id is not defined'.
i am sorry i do not understand. I pretty much cut and paste your code for the event listener instead of the code i used to listen for the onclick event therefore i do pass on the file_uniq_id variable.
Check the edit. I initially wrote file_unique_id, but the correct one is file_uniq_id - Notice the lack of ue in the variable name? I would suggest to read on event listeners.

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.