0

I'm creating something like GMAIL functionality with JavaScript ES5 ( I use only const and let, that's it, rest ES5).

So I manage to create the list functionality, all works except that when I select the items and move them to a different list, they lose any functionality, and I can't do anything with them.

I believe I need to use querySelectorAll to get all the lists, but that doesn't work. Not sure what should I do here.

I think I need to select all the lists, and then loop them to add interactivity.

CodePen: https://codepen.io/Aurelian/pen/dJryrX?editors=1010

JS:

window.onload = function() {

  //////////////////////////////////
    // VARIABLES
    //////////////////////////////////
  // Form
  const form = document.querySelector('#registrar');
  const input = form.querySelector('input');

  // Lists
  const partyList = document.querySelector('.party-lists');
  const partyInvitedList = document.querySelector('#list-invited')
  const partyGoingList = document.querySelector('#list-going');
  const partyNotSure = document.querySelector('#list-not-sure');
  const partyNotGoing = document.querySelector('#list-not-going');

  // List Options
  const listOptions = document.querySelector('.list-options');
  const btnMoveToGoing = document.querySelector('.btnMoveGoing');
  const btnMoveToNotSure = document.querySelector('.btnMoveNotSure');
  const btnMoveToNotGoing = document.querySelector('.btnMoveNotGoing');
  const btnDeleteSelected = document.querySelector('.btnDeleteSelected');


  //////////////////////////////////
    // FUNCTIONS
    //////////////////////////////////
  function createLI(text) {
    const li = document.createElement('li');
    const span = document.createElement('span');  
    span.textContent = text;
    li.appendChild(span);
    const label = document.createElement('label');
    const checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    label.appendChild(checkbox);
    li.appendChild(label);  
    const editButton = document.createElement('button');
    editButton.textContent = 'edit';
    li.appendChild(editButton);
    const removeButton = document.createElement('button');
    removeButton.textContent = 'remove';
    li.appendChild(removeButton);
    return li;
  }


  //////////////////////////////////
    // EVENT HANDLERS
    //////////////////////////////////
  form.addEventListener('submit', function(e) {
    e.preventDefault();
    const text = input.value;
    input.value = '';
    const li = createLI(text);
    partyInvitedList.appendChild(li);
  });

  partyList.addEventListener('click', function(e) {
    if (e.target.tagName === 'BUTTON') {
      const button = e.target;
      const li = button.parentNode;
      const ul = li.parentNode;
      if (button.textContent === 'remove') {
        ul.removeChild(li);
      } else if (button.textContent === 'edit') { 
        const span = li.firstElementChild;
        const input = document.createElement('input');
        input.type = 'text';
        input.value = span.textContent;
        li.insertBefore(input, span);
        li.removeChild(span);
        button.textContent = 'save';
      } else if (button.textContent === 'save') { 
        const input = li.firstElementChild;
        const span = document.createElement('span');
        span.textContent = input.value;
        li.insertBefore(span, input);
        li.removeChild(input);
        button.textContent = 'edit';
      }
    }
  });

  listOptions.addEventListener('click', function(e) {

    partyList.querySelectorAll('*:checked').forEach(function (listItems) {
      const button = e.target;
      var items = listItems.parentNode.parentNode;
      if(button.className === 'btnMoveGoing') {
          partyGoingList.appendChild(items);
          items.checked = false;
          var item = listItems;
          item.checked = false;
      } else if(button.className === 'btnMoveNotSure'){
          partyNotSure.appendChild(items);
          var item = listItems;
          item.checked = false;
      } else if(button.className === 'btnMoveNotGoing'){
          partyNotGoing.appendChild(items);
          var item = listItems;
          item.checked = false;
      } else if(button.className === 'btnDeleteSelected'){
         listItems.parentNode.parentNode.remove();
         var item = listItems;
         item.checked = false;
      } 
    });

  });


}

HTML:

    <div class="top">
<form id="registrar">
<input type="text" name="name" placeholder="Invite Someone">
<button type="submit" name="submit" value="submit">Submit</button>
</form>

<div class="list-options">
<button class="btnMoveGoing">Move to Going</button>
<button class="btnMoveNotSure">Move to Not Sure</button>
<button class="btnMoveNotGoing">Move to Not Going</button>
<button class="btnDeleteSelected">Delete Selected</button>
</div>

</div><!-- /top -->

<div class="col">
<h3>Invited</h3>
<ul id="list-invited" class="party-lists">

</ul>
</div>

<div class="col">
<h3>Going</h3>
<ul id="list-going" class="party-lists">

</ul>
</div>

<div class="col">
<h3>Not Sure</h3>
<ul id="list-not-sure" class="party-lists">

</ul>
</div>

<div class="col">
<h3>Not Going</h3>
<ul id="list-not-going" class="party-lists">

</ul>
</div>
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • What is your question? – Code-Apprentice Jan 22 '18 at 03:38
  • Yeah, I figured out that I need to loop throw the lists and add event listener to them, so I kinda answered it myself. But a better way to do this? – Aurelian Spodarec Jan 22 '18 at 03:39
  • What lists? Are you referring to another question that you posted? Not everyone reading this question will have read your previous one. Each question on this site is on its own page. They must stand on their own. If you figured something out, then post an answer to your original question. Only use the "Ask a Question" button if you have a new question. And then you must give enough information so that anyone can understand without any additional context. – Code-Apprentice Jan 22 '18 at 03:41
  • I see now. You edited your original question with stuff that made little sense on its own. If you found something that you think will be useful to future visitors then you should post an answer down below. If you don't think the answer will be useful to others than you can just delete this question. – Code-Apprentice Jan 22 '18 at 03:44
  • If you look at the code, it's below, you just need to expand the source code. After looking at it, it's obvious which list, as there is only one, or perhaps 1 repeated 4 times, the party-list
    This question stands on it's own, with no previous question. Thing is I solved it with making the lists to work, so now I'm wondering how could I make the code that's in the codepen better. I believe the way I wrote things, the way I communicated put you in confuse mode.
    – Aurelian Spodarec Jan 22 '18 at 03:46
  • When I first visited this page it had no code because you deleted it with your message that you had figured something out. I had to roll back to a version of your question which had your original question including the code you were asking about. – Code-Apprentice Jan 22 '18 at 03:47
  • Strange. I included the code straight off. I was making edits to the question when someone else was making edits to my question, I think there was some bug with it. Though now it sounds that it works fine. Not sure if this can be useful for other people, maybe for new people. I mean, I asked it but yeah. Do you know how could I make the code better? I mean, the loops I mean, this is kinda a mess with repeating. – Aurelian Spodarec Jan 22 '18 at 03:49
  • Probably just conflicting edits and not a bug with the site. Once you get a fully working example, you can ask for suggestions to improve your code on [codereview.se]. – Code-Apprentice Jan 22 '18 at 03:51
  • Should I delete this question here? It might be good for others, but I think this is such a basic question xd took me few minutes to figure out, but this is same as making tabs, kinda : p Thanks, I'll ask there. – Aurelian Spodarec Jan 22 '18 at 03:59
  • This question is less useful without any answers. Feel free to answer it yourself or delete it. – Code-Apprentice Jan 22 '18 at 04:00
  • In the future, double check your question after you finish editing it to make sure it makes sense without any additional context. There seems to have been some mixups with the editing process here and when I first visited this page it started with something like "I solved the problem..." without any actual statement of the problem. – Code-Apprentice Jan 22 '18 at 04:01
  • sorry about the edit confusion. You can add only one click event to the parent `
    ` and check the `event.target` to find which element was clicked. Code-Apprentice reverted the edit back to Revision 2, but you can see your other edits in the edit history
    – Slai Jan 22 '18 at 04:05
  • But now what I wrote it's gone as well. It's like someone deleted what I wrote from my question. – Aurelian Spodarec Jan 22 '18 at 04:05
  • Don't worry xd I was confused too, it happens xd – Aurelian Spodarec Jan 22 '18 at 04:06
  • So I can use the bubling way, instead of for loop? So the same way I did the other things, right? – Aurelian Spodarec Jan 22 '18 at 04:06
  • yes. I am guessing drag and drop might be easier for the users https://www.w3schools.com/html/html5_draganddrop.asp, https://jqueryui.com/draggable/#sortable – Slai Jan 22 '18 at 04:10
  • Oh, yeah, I will need to do that as well, the more features the better :D As I'm learning JS. I will try to do the drag and drop, I think my brain is thinking to know the answer on how that'll work :DDDDDDDD THIS IS SO AWESOME! :D Can't wait to make the drag and drop! :D My battery will die damn. Will do this when I wake up and pc charges :D Thanks :D – Aurelian Spodarec Jan 22 '18 at 04:13
  • *I use only const and let, that's it, rest ES5* - what's the purpose of this limitation? It's still ES6. – Estus Flask Jan 22 '18 at 05:31
  • Well, only const and let is ES6. Arrow functions etc.. they aer ES6, I don't use them. It's better to learn ES5 first. And I preffer to learn gradually about ES6, instead of going through a tutorial on all the new features when that woudn't make sense as I would forget it, so just getting comfortable with const and let ^^ – Aurelian Spodarec Jan 22 '18 at 14:05

0 Answers0