I'm working on removing all jQuery code from an application in place of pure Javascript. I've been working through it and making the changes which are working fine, but the code itself seems a lot "more" than jQuery.
I get the feeling that there is a much cleaner way to write this code than my approach.
Below is an extract from the code:
var aTestUl = document.createElement('ul');
var aTestLi = document.createElement('li');
var bTestBtn = document.createElement('a');
bTestBtn.className = "gc_bTest";
bTestBtn.id = "gc_bTest_" + aTestVarId;
bTestBtn.href = "#";
bTestBtn.dataset.body = aTestVarBody;
bTestBtn.innerHTML = "Call test function 1";
var bTestCTestDiv = document.createElement('span');
bTestCTestDiv.innerHTML = " - ";
var cTestBtn = document.createElement('a');
cTestBtn.className = "gc_cTest";
cTestBtn.id = "gc_cTest_" + aTestVarId;
cTestBtn.href = "#";
cTestBtn.dataset.body = aTestVarBody;
cTestBtn.innerHTML = "Call test function 2";
var aTestText = document.createTextNode(aTestVarId + ' - '
+ aTestVarBody + ' - ');
aTestLi.appendChild(aTestText);
aTestLi.appendChild(bTestBtn);
aTestLi.appendChild(bTestCTestDiv);
aTestLi.appendChild(cTestBtn);
aTestUl.appendChild(aTestLi);
document.getElementById('gc_aTest_list').appendChild(aTestUl);
This essentially creates a list with a heading: "someId - someBody - Function 1 href - Function 2 href". There is some more code not included which appends another ul and li... to the end of aTestLi before it appends to aTestUl.
This was just a few lines of code in jQuery. Is there a much simpler way to do this?
As a breakdown, incase the code is a bit confusing, it does the following:
1. Create a master <ul> to append to the master <div>
2. Create a list <li> to append to the master <ul>
3. Create some text to put in the <li>
4. Create an <a> element (with attributes, such as id, class, ...)
5. Create another <a> element (with similar attributes)
6. Put the text, hyphens and the two <a> elements in the list
(<li>TEXT - <a /> - <a /></li>)
7. Attach the <li> to the master <ul>
(<ul><li>TEXT - <a /> - <a /></li></ul>)
8. Attach the master <ul> to the master <div>
(<div><ul><li>TEXT - <a /> - <a /></li></ul></div>)
This is something the code does quite often in the application so if it can be simpler would be great.
Thanks!
EDIT: jQuery and other external libraries are not valid for this application due to certain requirements.
EDIT: To use correct terminology, "Vanilla Javascript" is the requirement.
EDIT: HTML output
<div class="gc_list" id="gc_profile_list">
<ul>
<li>
ID9723 - AA00BB11CC2 -
<a class="gc_f_values" id="gc_f_values_ID9723" href="#" data-device="AA00BB11CC2">Get F Values</a>
<span> - </span>
<a class="gc_i_values" id="gc_i_values_ID9723" href="#" data-device="AA00BB11CC2">Get I Values</a>
<ul>
<li>Action: Save</li>
<li>Action: Delete</li>
<li>Action: Archive</li>
<li>Action: Pass</li>
</ul>
</li>
</ul>
</div>