How do I make my application understand which delete button I am clicking and delete the right list?

Asked

Viewed 28 times

-5

I have an app that adds a list for the person to put a new schedule every time they click on a button, that new list comes with a delete button on the side, but when the person adds more than one list, and tries to delete the last one, it always deletes the first one that was included, it doesn’t seem to identify which button is being clicked.

The delete script is as follows:

function deleteField() {
  const fieldFather = document.querySelector('#schedule-items')
  const fieldSon = document.querySelector('#second-schedule-div')
  fieldFather.removeChild(fieldSon)
}

document.querySelector('.delete-button').addEventListener('click', deleteField)

and in the list that is added, the buttons have a "onClick='deleteField()'"

the code I use to add the Divs is: OBS.: the div that I clone, it is in HTML with display None

document.querySelector('#add-time')
  //quando clicar no botão chama o cloneField
  .addEventListener('click', cloneField)


function cloneField() {
  ?
  const newFieldContainer = document.querySelector('#second-schedule-div').cloneNode(true)
  
  const fields = newFieldContainer.querySelectorAll('input')
  
  fields.forEach(field => {
    field.value = ''
  });
  
  document.querySelector('#schedule-items').appendChild(newFieldContainer)
}

html code:

<fieldset id='schedule-items'>
          <legend>Horários disponíveis
            <button type='button' id='add-time'>+ Novo Horário</button>
          </legend>

          <div id='main-schedule-div' class="schedule-item">
            <div class="select-block">
              <label for="weekday">Dia da semana</label>
              <!--name no select dessa forma, diz ao back end que pode ter mais de um valor-->
              <select name="weekday[]">
                <option value="">Selecione uma opção</option>

                {%for weekday in weekdays %}
                <option value="{{loop.index0}}">
                  {{weekday}}
                </option>
                {%endfor%}

              </select>
            </div>
            <div class="input-block">
              <label for="time_from">Das</label>
              <input type="time" name='time_from[]' required>
            </div>
            <div class="input-block">
              <label for="time_to">Até</label>
              <input type="time" name='time_to[]' required>
            </div>
          </div>  


        </fieldset>

html from cloned div:

<div id='hidden-div'>
    <div id='second-schedule-div' class="schedule-item">
      <div class="select-block">
        <label for="weekday">Dia da semana</label>
        <!--name no select dessa forma, diz ao back end que pode ter mais de um valor-->
        <select name="weekday[]">
          <option value="">Selecione uma opção</option>

          {%for weekday in weekdays %}
          <option value="{{loop.index0}}">
            {{weekday}}
          </option>
          {%endfor%}

        </select>
      </div>
      <div class="input-block">
        <label for="time_from">Das</label>
        <input type="time" name='time_from[]' required>
      </div>
      <div class="input-block">
        <label for="time_to">Até</label>
        <input type="time" name='time_to[]' required>
      </div>
      <div id='div-button'>
        <button type='button' class='delete-button' onClick='deleteField()'>delete</button>
      </div>
    </div>
  </div>

application photo:

Foto da aplicação

  • You are deleting the image by the id, which in your case is the same for all items (id must be unique) the browser reads from top to bottom, and in the first occurrence executes the action, so the first one is deleted. You need to dynamically assign unique id’s. Do not print the code, make it easy to test and execute your code, paste the code and use the tags, your question is negatively so.

  • 1

    Ah yes, I get it, sorry, I’m new here. I’ll edit the post.

  • How do I generate ID’s dynamically and so the application understands when I press the button, which refers to that ID?

  • How do you generate the Divs? Paste the code of how the HTML generates in the question.

  • Okay, here are the codes.

  • I changed the approach, ta in the answer.

Show 1 more comment

1 answer

0


I believe the easiest way without changing your code too much is to search for the parent elements and remove. I changed the delete function.

   function deleteField(t) {
      t.parentNode.parentNode.parentNode.removeChild(t.parentNode.parentNode);
   }

On the button, pass yourself as parameter

<button type='button' class='delete-button' onClick='deleteField(this)'>delete</button>

The one in the Idden div, they’ll all be cloned this way

  • Man, thank you so much, it worked perfectly, you’re wild!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.