Hello, I am new to javascript and I am not used to creating nodes…
So, I have a <div id="incontent"></div>
in which I am generating elements using javascript… here is my script :
var lastId = 0; // initialise ID
function MyFunction( var1, var2, var3) {
const incontent = document.getElementById('incontent');
incontent.innerHTML+= var1 + var2 + (var3 ? ' (var3=' + var3 + ')' : '');
if (var3 === 'descforalink') {
incontent.innerHTML += `<div class="contentLink"><p>a link</p></div></div>`;
}
}
//manages the click on possible
document.getElementById('incontent').addEventListener('click', function (e) {
lastId++ ; // for a different ID
});
// element in which to instantiate the contentDATA for another rendering...
const contentDATA = document.getElementById('contentDATA');
// contentDATA content is received
contentDATA.addEventListener(MyMouv.Descperso, ({ data }) => {
var divques1 = '<div class="contentDesc col-12"><p>';
var divques2 = '</p>';
var divques3 = '</div>';
var contentallbtns = '';
var contentbtnsbefore = '<div class="col-12 contentLink">';
var contentbtnsafter = '</div></div>';
MyFunction('', divques1 + data.text + divques2 , data.var3, divques3);
// if actions buttons/links
if (data.actions) {
var btns = '';
for (var i = 0; i < data.actions.length; i++) { if (i> 0) {btns += ' ';}
let act = data.actions[i];
btns += '<a class="mybtn"' + ' data-val="' + act.value + '">' + act.title + '</a>';
contentallbtns = contentbtnsbefore + btns + contentbtnsafter;
}
MyFunction('',contentallbtns);
}
//building tags
var startB = document.createElement('section');
var myiddesc = 'iddesc-'+lastId ;
startB.setAttribute('id', myiddesc );
startB.setAttribute('class', 'thesection');
document.getElementById('incontent').appendChild(startB);
var startB2 = document.createElement('div');
startB2.setAttribute('class', 'container');
startB.appendChild(startB2);
var startB3 = document.createElement('div');
startB3.setAttribute('class', 'row MyRow');
startB2.appendChild(startB3);
document.querySelector('div.MyRow').appendChild(document.querySelector('div.contentDesc'));
document.querySelector('div.MyRow').appendChild(document.querySelector('div.contentLink'));
});
This generates the first time a structure of this type and this is what I want:
<section id="iddesc-0" class="thesection">
<div class="container">
<div class="row MyRow">
<div class="contentDesc">
<p>Hello World</p>(var3=descforalink)
</div>
<div class="contentLink">
<p>a link</p>
</div>
</div>
</div>
</section>
but when another section is created, with other content, this structure appears:
<div class="contentDesc">
<p>Hello World</p>(var3=descforalink)
</div>
<div class="contentLink">
<a class="mybtn" data-val="banana">banana</a><a class="mybtn" data-val="orange">orange</a>
</div>
<section id="iddesc-1" class="thesection">
<div class="container">
<div class="row MyRow">
</div>
</div>
</section>
So my div class="contentDesc"
and my div class="contentLink"
is well created … but is no longer the child of my div class="row MyRow"
.
appendChild() only works once (when the first section is created)… I don’t understand, because for me, the tags are created each time via MyFunction, therefore present in the DOM, so appendChild () should move my items every time, but no
So, how can i change that? Always place my div div class="contentDesc"
and class="contentLink"
in the div class="row MyRow"
? Thank you for your help…