How do I make an delete button disappear after it appears when it is called in a click-like addeventlistener?

Asked

Viewed 47 times

-3

I would like the "remove time" button on the page not to appear again!

  • The page comes with an already time field
  • The remove time button does not appear on the rendered page
  • When the user clicks on the "add a time" button a new time field appears and the remove button appears right next to the add button
  • I would like the Remove Time button to disappear again if the user deletes the add field by leaving only one time as initially on the loaded page

Excerpt from html buttons

<fieldset id="schedule-items">
          <legend>Horários disponíveis
            <button type="button" id="add_time">+ Novo horário</button>
            <button type="button" id="rm-time">- Remover</button>
          </legend>
          <div class="schedule-item">
            <div class="select-block">
              <label for="weekday">Dia da semana</label>
              <select name="weekday[]" required>

Well, that’s what I implemented in JS but I can’t continue from there, so the remove button goes back to in 'Hide' mode say so, after the user deletes the added time

document.getElementById('rm-time').style.display = 'none'

document.getElementById('add_time').addEventListener('click', showRemoveField)

function showRemoveField() {
  const getSchedules = document.querySelectorAll('.schedule-item')
  const changeDisplay = document.getElementById('rm-time').style.display = ''

  if (getSchedules.length > 1) {
    return changeDisplay 
  } else {
    
  }
}

2 answers

1

Good thanks to the reply of the friend

@Rodrigo Carvalho de Brito

Follows answer from the same:

$('add_time').on("click", function(){
    $('rm-time').show();
});

 $('rm-time').on("click", function(){
    $(this).hide();
});
  • For a future doubt like mine I’ll leave as I did so that more elaborate codes can appear than mine to help those who may have the same doubt

  • rm-time is the ID of the Remove Time button

  • add_time is the ID of the add time button

  • Using this code the add time button "calls" to remove and the same disappears if the user wants to delete a time field in a way that leaves only one.

  • Using a condition was necessary because if the user put for example 3 time fields, using the direct Hide code on the rm-time button the same would disappear after being clicked preventing the removal of the second time added

$('#rm-time').hide();
$('#add_time').on("click", showRemoveField)


 function showRemoveField() {
  const showButton = document.querySelectorAll('.schedule-item')

  if (showButton.length > 1) {
    return $('#rm-time').show()
  } else {
}
}

$('#rm-time').on("click", hideRemoveField)

function hideRemoveField() {
  const hideButton = document.querySelectorAll('.schedule-item')
  if (hideButton.length <= 1) {
    return $('#rm-time').hide()
  } else {

  }
}

0

Your question is very confusing. If you use jQuery it will be much simpler, simple!!!

$('rm-time').hide();
$('add_time').on("click", function(){
    $('rm-time').show();
});

To hide the rm-time:

$('rm-time').on("click", function(){
    $(this).hide();
});

  • Carvalho, thanks for the answer, I improved my question, in fact the doubt is very simple, I think I ended up leaving very confused trying to explain too, I’m starting now in this world of programming, forgive me but I didn’t even know jQuery and by your comment I was amazed how easy it was to do with him everything I did with JS by itself and I’m looking for more related content, but then in the section you put was exactly what I have implemented, i would like if the user removes a time leaving only one, the remove button returns to the IDE()

  • I reissued the answer. Just by clicking the remove button add an Hide event to it itself.

  • Thank you very much, I managed to do what I wanted to do from a look at the answer I left for you to better understand what I needed ^^

  • Beauty! There are several ways to get to the goal and you got it. It was readable, this is important to allow future maintenance. If you can, from a carefully studied jQuery because it simplifies a lot of JS, however, the root JS will enable you to create a world of cool things!

Browser other questions tagged

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