0

Here is my code:

Template = {
    create_progress_div : function(uniqueIdentifier){
            document.getElementById('modal-content').innerHTML = '';
            document.getElementById('modal-content').innerHTML = ''+
                '<div class="pi-flow-file">'+
                    '<label name="pi-flow-text">'+uniqueIdentifier+'</label>'+
                    '<progress name="pi-flow-bar" id="'+uniqueIdentifier+'" max="100" value="0"></progress>'+
                    '<div name="pi-flow-action" nowrap="nowrap">'+
                        '<button href="#" onclick="API.upload_to_ad('+uniqueIdentifier+')">'+
                            '<img src="js/flow/resume.png" title="Resume upload" />'+
                        '</button>'+
                    '</div>'+
                '</div>';
        },
}

As you can imagine, with my API.upload_to_ad() function, I am getting a <uniqueIdentifier-string> is undefined.

I need to pass uniqueIdentifier as a string rather than an identifier for this to work.

How do I do this?

5
  • = ''+ '<div class="pi-flow-file">' ?? Why the '' if you are just going to concatenate another string? Commented Apr 23, 2018 at 22:32
  • for readability Commented Apr 23, 2018 at 22:33
  • It looks like you are concatenating uniqueIdentifer into your string properly. The real question is have you checked that uniqueIdentifier is actually being passed? Commented Apr 23, 2018 at 22:33
  • How does the extra '' add to the readability? Your adding more concatenation that doesn't need to be there. Commented Apr 23, 2018 at 22:34
  • And why document.getElementById('modal-content').innerHTML = ''; just prior to the next line that resets the innerHTML anyway? Commented Apr 23, 2018 at 22:36

2 Answers 2

4

You probably just need to add quotes to your argument, otherwise it is interpreted as an identifier:

'<button href="#" onclick="API.upload_to_ad(\'' + uniqueIdentifier + '\')">'+
     '<img src="js/flow/resume.png" title="Resume upload" />'+
 '</button>'+

Note that you have to escape those single quotes.

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

Comments

0

Inline event handlers are essentially eval inside HTML markup - they're bad practice and result in poorly factored, hard-to-manage code. You should attach the listener properly using Javascript instead:

Template = {
  create_progress_div : function(uniqueIdentifier){
    const modalContent = document.getElementById('modal-content');
    modalContent.innerHTML = 
      '<div class="pi-flow-file">'+
      '<label name="pi-flow-text">'+uniqueIdentifier+'</label>'+
      '<progress name="pi-flow-bar" id="'+uniqueIdentifier+'" max="100" value="0"></progress>'+
      '<div name="pi-flow-action" nowrap="nowrap">'+
      '<button href="#" onclick="API.upload_to_ad('+uniqueIdentifier+')">'+
      '<img src="js/flow/resume.png" title="Resume upload" />'+
      '</button>'+
      '</div>'+
      '</div>';
    modalContent.querySelector('button')
      .addEventListener('click', () => API.upload_to_ad(uniqueIdentifier));
  }
}

You also might consider using template literals instead, they're a lot easier to work with than manually concatenating strings over multiple lines:

Template = {
  create_progress_div : function(uniqueIdentifier){
    const modalContent = document.getElementById('modal-content');
    modalContent.innerHTML = `
      <div class="pi-flow-file">
      <label name="pi-flow-text">${uniqueIdentifier}</label>
      <progress name="pi-flow-bar" id="${uniqueIdentifier}" max="100" value="0"></progress>
      <div name="pi-flow-action" nowrap="nowrap">
      <button href="#" onclick="API.upload_to_ad(${uniqueIdentifier})">
      <img src="js/flow/resume.png" title="Resume upload" />
      </button>
      </div>
      </div>`;
    modalContent.querySelector('button')
      .addEventListener('click', () => API.upload_to_ad(uniqueIdentifier));
  }
}

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.