0

Can I use an eventListener and getElementsByClass to create a function that does the same for these buttons instead of having a function for each one? Any explanations will help, thank you!

     <button class = "Button" onclick = "Ginga()"><span id = "Ginga" class = "Movements" >ginga</span></button>
     <button class = "Button" onclick = "Base()"><span id = "Base" class = "Movements" >base</span></button>
     <button class = "Button" onclick = "Role()"><span id = "Role" class = "Movements" >role</span></button>
     <button class = "Button" onclick = "Negativa()"><span id = "Negativa" class = "Movements" >negativa</span></button>
    <div id = "SequenceContainer">
    <div id = "Sequence"></div>
    </div>

function Ginga() {
    var Ginga = document.getElementById('Ginga');
    var GingaCln = Ginga.cloneNode(true);
    Sequence.appendChild(GingaCln);
}
function Base() {
    var Base = document.getElementById('Base');
    var BaseCln = Base.cloneNode(true);
    Sequence.appendChild(BaseCln);
}
function Role() {
    var Role = document.getElementById('Role');
    var RoleCln = Role.cloneNode(true);
    Sequence.appendChild(RoleCln);
}
function Negativa(){
    var Negativa = document.getElementById('Negativa');
    var NegativaCln = Negativa.cloneNode(true);
    Sequence.appendChild(NegativaCln);
}
  • 3
    Use [event delegation](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of assigning multiple event listeners — it’s more maintainable, and applies to dynamically added elements. E.g., use an [event argument](//developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#The_event_listener_callback)’s [`target`](//developer.mozilla.org/en-US/docs/Web/API/Event/target). See [the tag info](/tags/event-delegation/info) and [What is DOM Event delegation?](/q/1687296/4642212). You could use `data-*` attributes to store the target ID. – Sebastian Simon May 27 '21 at 05:40

3 Answers3

2

Sure you can.

Change the onClick event with:

onclick = "justOne(this)"

(this is used to send to the script the element that the user clicked on)

And replace the javascript with just one function:

function justOne(element) {
  Sequence.appendChild(element.children[0].cloneNode(true));
}

(we select the first child of the clicked element and clone it)

That's it!

LE: I've added another answer with a different approach, using eventListener and getElementsByClassName, as you asked in the question.

Cosmin Staicu
  • 1,514
  • 2
  • 18
  • 21
0
     <button class = "Button" onclick = "OneForAll()"><span id = "Ginga" class = "Movements" >ginga</span></button>
     <button class = "Button" onclick = "OneForAll()"><span id = "Base" class = "Movements" >base</span></button>
     <button class = "Button" onclick = "OneForAll()"><span id = "Role" class = "Movements" >role</span></button>
     <button class = "Button" onclick = "OneForAll()"><span id = "Negativa" class = "Movements" >negativa</span></button>
    <div id = "SequenceContainer">
    <div id = "Sequence"></div>
    </div>

function OneForAll(event) {
  var element = event.target;
  var elementCln = element.cloneNode(true);
  Sequence.appendChild(elementCln);
}
0

You can also do it with eventListener and getElementsByClass (as you asked) like this:

<button class = "Button"><span id = "Ginga" class = "Movements" >ginga</span> </button>
<button class = "Button"><span id = "Base" class = "Movements" >base</span></button>
<button class = "Button"><span id = "Role" class = "Movements" >role</span></button>
<button class = "Button"><span id = "Negativa" class = "Movements" >negativa</span> </button>
<div id = "SequenceContainer">
<div id = "Sequence"></div>
</div>

<script>
[].forEach.call(document.getElementsByClassName("Button"), function (element) {
    element.addEventListener("click", function () {
        Sequence.appendChild(this.children[0].cloneNode(true));
    });
});
</script>

Please note that I have removed the 'onclick' event inside the <button> element, as it is no longer needed.

This method works like this:

  1. We call the forEach method of the Array class, to iterate over an "array like" element: [].forEach.call()
  2. The list of buttons is obtained using document.getElementsByClassName("Button")
  3. We add an event listener to each button using the element.addEventListener method;

Keep in mind that the script has to be inserted after the button definitions. If you insert the script in the head section, it will not work, because the script runs but the DOM in not yet defined (there are no buttons to attach events to).

Cosmin Staicu
  • 1,514
  • 2
  • 18
  • 21