JavaScript Append Elements vs. innerHTML append getelementbyid innerhtml,element append innerhtml,jquery append element innerhtml

I want to know if it is more correct to use innerHTML:

function createModal(){
   modal = document.createElement('div');
   modal.id = "modal";
   modal.className = "modal";
   modal.innerHTML = "<div id='modal-content' class='modal-content'><div id='modal-header' class='modal-header'><h2 id='title'>Login...</h2></div><div class='modal-body'><div class='loginContainer'><div class='loginWrapper'><div class='inpWrapper'><input name='h' placeholder='Hostname' onkeyup='enter();' autocapitalize='none' autofocus><span class='focus-border'></span></div><div class='inpWrapper'><input name='u' placeholder='Username' onkeyup='enter();' autocapitalize='none'><span class='focus-border'></span></div><div class='inpWrapper'><input type='password' name='p' placeholder='Password' onkeyup='enter();' autocapitalize='none'><span class='focus-border'></span></div></div></div><p>*You only need to log in once so you can close out of any login prompts on other pages</p></div><div class='modal-footer'><button class='save-changes' onclick='login();'>Connect</button><button class='close-modal' onclick='removeModal()'>Close</button></div></div>";
   document.body.appendChild(modal);
}

or appendChild() and create and append each child individually:

function createModal(){
    modal = document.createElement('div');
    modal.id = "modal";
    modal.className = "modal";

    var modalContent = document.createElement('div');
    modalContent.id = "modal-content";
    modalContent.className = "modal-content";

    var modalHeader = document.createElement('div');
    modalHeader.id = "modal-header";
    modalHeader.className = "modal-header";

    var title = document.createElement('h2');
    title.id = "title";
    title.innerText = "Login...";
    modalHeader.appendChild(title);
    modalContent.appendChild(modalHeader);

    var modalBody = document.createElement('div');
    modalBody.id = "modal-body";
    modalBody.className = "modal-body";

    var loginContainer = document.createElement('div');
    loginContainer.className = "loginContainer";

    var loginWrapper = document.createElement('div');
    loginWrapper.className = "loginWrapper";

    var inpWrapper = document.createElement('div');
    inpWrapper.className = "inpWrapper";

    var focusBorder = document.createElement('span');
    focusBorder.className = "focus-border";

    var inpWrapper2 = document.createElement('div');
    inpWrapper2.className = "inpWrapper";

    var focusBorder2 = document.createElement('span');
    focusBorder2.className = "focus-border";

    var inpWrapper3 = document.createElement('div');
    inpWrapper3.className = "inpWrapper";

    var focusBorder3 = document.createElement('span');
    focusBorder.className = "focus-border";
    inpWrapper.appendChild(focusBorder3);

    modalContent.appendChild(modalBody);
    modal.appendChild(modalContent);
    document.body.appendChild(modal);
}

Which would look something like that but I didn't type every element out because I want to know which is better. I just quickly typed that out so you would understand what I mean. Thanks.

Answer:1

Both are fine, and still neither is perfect for all purposes.

  • For the innerHTML version: if you feed it bad HTML, then you won't know until you look at the result. And in case of really complicated HTML (e.g. coming from a CMS) then the parsing of it may take time that becomes noticeable. Also you need to take care of escaping your outer quotes if need to use them inside the HTML. But it will be very clear what you are creating.

  • For the programmed version: it won't get any faster than that, it may be more exact and debuggable, it may be easier to change (e.g. add a div layer somewhere, or move an element around) but it is also hard to guess from examining the source what you are actually creating.

So all in all, it depends.

Answer:2

I have a working graph which displays some nodes & their attributes. Then I get a JSON with different data, where some nodes may already exist on my graph. How to combine both data sources, to ...

I have a working graph which displays some nodes & their attributes. Then I get a JSON with different data, where some nodes may already exist on my graph. How to combine both data sources, to ...

I wrote a code, that make the button not disabled when you check at least one checkbox with class "sum". I want to change the code, so I have to classes for and you can check only one checkbox (or ...

I wrote a code, that make the button not disabled when you check at least one checkbox with class "sum". I want to change the code, so I have to classes for and you can check only one checkbox (or ...

  1. javascript disabled button still clickable
  2. javascript disabled button onclick
  3. javascript disabled button
  4. javascript disabled button by class
  5. javascript disabled button not working
  6. javascript disabled button color
  7. javascript disabled button css
  8. javascript disabled button html
  9. javascript button disabled enabled
  10. javascript button disabled attribute
  11. javascript button.disabled false not working
  12. javascript button disabled true
  13. javascript button disabled property
  14. javascript button disabled false
  15. javascript button disabled not working in chrome
  16. javascript button disabled attr
  17. javascript set disabled button
  18. javascript remove disabled button
  19. javascript toggle disabled button
  20. javascript disabled radio button

I'm trying to use WebCola http://marvl.infotech.monash.edu/webcola/ but I can't find a link to the real documentation. The website seems to have only a few examples and some (apparently) auto-...

I'm trying to use WebCola http://marvl.infotech.monash.edu/webcola/ but I can't find a link to the real documentation. The website seems to have only a few examples and some (apparently) auto-...

I've created a simple JavaScript statement to open and close the nav side bar but it will only successfully open it and I'm confused as to why? if someone could explain it would be very helpful <...

I've created a simple JavaScript statement to open and close the nav side bar but it will only successfully open it and I'm confused as to why? if someone could explain it would be very helpful <...

  1. simple javascript statement
  2. simple javascript if statement
  3. simple switch statement javascript
  4. simple case statement javascript
  5. simple if else statement javascript