JavaScript.To remove() using parent(), .child()? (In shoveling)

Asked 2 years ago, Updated 2 years ago, 46 views

Hello, I started shoveling, so I'm uploading it. ㅠ<

<div>
    <div id="noteForm"+num>....</div>
    <script>....</script>
    <select>...</select>
    <input type='text' ......>
    <div class="dhxform_base">....</div>
</div>
var newSelect = $(this)
     .clone()
     .attr({name:'select_form_'+counter, id:'noteForm'+counter})
     .appendTo($(this).parent())
     .change(onChange)
     .children("select").attr('name','select_options_'+counter)
     .parent().children("input").attr({name:'run_memo'+counter,id:'run_memo'+counter})
    // It doesn't look like you need to see it because it's done without any problems.

    // Below, modify the noteForm tag id and name and delete all of them inside.
     .parent().children("#noteForm"+(counter-1)).attr({name:"noteForm"+counter, id:"noteForm"+counter}).children().remove()

     /*It works well on the top, but it cannot be deleted from the department below.ㅠ*/
     .parent().children("script").remove()
     .parent().children("div[class=dhxform_base]").remove();
$("#noteForm"+counter).append(sc);

I can't delete it even if I change any number after using remove() using parent() and child(). I don't know why, but I'm shoveling. ㅠ<

jquery html javascript php

2022-09-22 20:58

1 Answers

First of all, it is not clear where this is, so if I answer assuming that the top root, DIV tag, is this.

Remove() is a function of deleting the selected element, but looking at the source, it seems that you want to delete only the child element of the selected element. It's better to use empty().

Try using addBack(). I think you can use siblings() to select the sibling element.

If you say you can't delete it, it's because you selected the parent element based on the deleted element.

.parent().children("#noteForm"+(counter-1))
.attr({name:"noteForm"+counter, id:"noteForm"+counter}).children().remove() 
// remove() returns the selected element, so the .child() method in this line is the same as the evaluated result, i.e. the deleted element is selected

.parent().children("script").remove() // the parent of the deleted element does not exist.
.parent().children("div[class=dhxform_base]").remove(); // parent of deleted element does not exist.

So you can change it to one of the three methods below:

// #1
.parent().children("#noteForm"+(counter-1)).attr({name:"noteForm"+counter, id:"noteForm"+counter}).children().remove()
.end().end().children("script").remove()

// #2
.parent().children("#noteForm"+(counter-1)).attr({name:"noteForm"+counter, id:"noteForm"+counter}).empty()
.end().children("script").remove()

// #3
.parent().children("#noteForm"+(counter-1)).attr({name:"noteForm"+counter, id:"noteForm"+counter}).empty()
.siblings("script").remove()

For your information, since what is returned after the last deletion operation is a deleted element, allocating it to the variable newSelect becomes unnecessary code.


2022-09-22 20:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.