Matheus Santos Matheus Santos - 4 months ago 26
jQuery Question

Javascript appendChild if div not exists

i've working in a big project, and that i want is

if div 1 not contain div 2 child{

div1.appendChild(div2)

}


But, i'm getting problem to solve this

My code

<script>
dc = document.createElement("div");
dc.className = "doctor_card doctor-"+o+" hidden";
dcc.appendChild(dc);
</script>


Thanks for advice :)

Tyr Tyr
Answer

Try this:

<script>
var className = "doctor_card doctor-"+o+" hidden";

if (dcc.getElementsByClassName(className).length == 0) {
    dc = document.createElement("div");
    dc.className = className;
    dcc.appendChild(dc);
}
</script>