简体   繁体   中英

how can I add a copy of a div with jquery?

I have a div containg a few dynamically generated dropdowns.

I want to clone this and add it underneath the original. The code for the original is:

<div id="subjectselectiondiv" style="width:inherit;">
    <h4>Select the subjects that you are able to tutor</h4>
    <div id='choices'>
        <script type="text/javascript">
            var doc = document.getElementById("choices");
            var subj_div = document.createElement('div');
            subj_div.setAttribute('class', 'selectSearchBar');
            subj_div.innerHTML = '<select id="level'+counter+'" name="level'+counter+'" class="selectSearchBar"><?php echo "<option>Level</option>"; while($row = mysqli_fetch_array($result_level, MYSQLI_ASSOC)){echo "<option value=".$row['id'].">".$row['level']."</option>";}?></select><div id="subject'+counter+'" style="display:inline;">sBar2</div><div id="topic'+counter+'" style="display:inline;">sbar3</div>';
            doc.appendChild(subj_div);
        </script>
    <a href='#' class="more" onclick="Repeat()">Add another subject</a>
</div>

I thought that the function Repeat() would work with:

function Repeat(){
    counter++;
    $('#choices').clone().appendTo('#subjectselectdiv');
    event.preventDefault();
}

but this isnt working - am I missing something?

Thanks

Name mismatches:

<div id="subjectselectiondiv" style="width:inherit;">
                     ^^^^^^
$('#choices').clone().appendTo('#subjectselectdiv');
                                            ^^^

What you're attempting to append to doesn't exist. Most likely if you'd bothered checking your javascript console, you'd have seen the warnings/errors about the non-existing element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM