Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript How to add data-id to button, allowing me to remove the 2nd container

Can you help me?

The whole goal or overarching objective here is being able to remove the 2nd container from the code.

My HTML is repetitive. Since I only show one player at a time, I should only have a single container.

I’m already using data-container on each button to figure out which video container I want to play.

Instead I want to have a single video player element/container, and just pass the corresponding data-id directly from the button instead. Where I can then instantiate the video player in the same process to load only the video that is clicked.

<div class="video video-frame">

So for example, I’m using play2 to go find a video container named play2 and doing actions to show that specific container. In that container I have an element that has the data-id of my YouTube video -Xgi_way56U

So instead of creating a playX container for every single video, I want to put -Xgi_way56U on the button instead. When the button is clicked, I want to take that ID and pass it into the video player element to load it on demand.

The idea here would be to attach the id data-id="-Xgi_way56U"

to the button:

<button class="playa2 cover" type="button" data-container="play1"></button>

Which would then become this:

<button class="playa2 cover" type="button" data-container="play1" data-id="-Xgi_way56U"></button>

I have been told by multiple people now, I should only be needing 1 container.

They said it is unnecessary duplication where only 1 container is all that should be needed.

The idea here would be to add the data-id="" to the buttons, which should allow me to remove the 2nd container from the code.

I have not been able to figure out how to do this.

How the code works is, after clicking a button a video will appear on the screen, click the X the buttons return to the screen where you can click on the 2nd button and a video will appear on the screen.

Two buttons should be able to run off of one of these, that are inside 1 container, instead of needing multiple containers for each button.

<div class="video video-frame">

Here is the code working with 2 containers: https://jsfiddle.net/7apg90wz/

Code:
<div class="container play1 with-curtain">
  <div class="inner-container curtain ">
    <div class="ratio-keeper">
      <div class="wrap">
        <div class="video video-frame" data-id="-Xgi_way56U"></div>
      </div>
    </div>
    <button class="exit" type="button" title="Exit" aria-label="Close"></button>
  </div>
</div>
<div class="container play2 with-curtain">
  <div class="inner-container curtain">
    <div class="ratio-keeper">
      <div class="wrap">
        <div class="video video-frame" data-id="0dgNc5S8cLI"></div>
      </div>
    </div>
    <button class="exit" type="button" title="Exit" aria-label="Close"></button>
  </div>
</div>

Here is the container with buttons:
Code:
<div class="playButtonContainer with-curtain">
  <button class="playa1 cover" type="button" data-container="play1"></button>
  <button class="playa2 cover" type="button" data-container="play2"></button>
</div>

Removing 1 container I have this now: https://jsfiddle.net/82rxzq7h/

The code is not working now because more stuff is needed to be done.

I don’t know what else is needed to be done, or how to achieve this, of being able to remove the 2nd container from the code.

I have been having a very difficult time trying to figure out how to do this in the code.

Code:
<div class="container play1 with-curtain">
  <div class="inner-container curtain">
    <div class="ratio-keeper">
      <div class="wrap">
        <div class="video video-frame"></div>
      </div>
    </div>
    <button class="exit" type="button" title="Exit" aria-label="Close"></button>
  </div>
</div>

Here is the container with buttons: Where I attached the data-id="" to them.
Code:
<div class="playButtonContainer with-curtain">
  <button class="playa1 cover" type="button" data-container="play1" data-id="-Xgi_way56U"></button>
  <button class="playa2 cover" type="button" data-container="play1" data-id="0dgNc5S8cLI"></button>
</div>

I tried to provide as much detail in here as possible in what I am trying to do in the code.

If you may have any questions, or would like to know more information, please ask.
 
Solution
What can be done to figure out how to fix it?
OK I Got it
1669670649074.png

1669670845919.png

Also, sorry for the late reply, been in and out of meetings all for the most part of today. Not gonna lie, as I was going through your code, I found myself a bit confused at times due to the naming conventions you use. Go ahead and make these changes, and clean up the code a little as far as those console logs and those functions you commented out, and you should be good to go!
NOTE: Please try to use better naming conventions.
Also, following my own advice, and separating all the code into its respective files, I get this error with I run it locally
View attachment 1932
It seems to be coming from this part of your js code
View attachment 1933

Any idea as to why?

The idea here would be to somehow remove someArr, from the code?

Without errors appearing in the code.

Did I fix any of those errors in here? https://jsfiddle.net/ystz8cx0/

JavaScript:
totalP.forEach(function (someArr,index) {
      console.log(someArr[index]);
        players.add(".playSingle" + index, (playerVarsList[index] || {}));
    });

I am seeing this:

that is all I am seeing.png
 
Last edited:
Again, why are you replacing your original "_" parameter and now calling it "someArr"? Coming back to my original question... why is it necessary?

Doing this:

https://jsfiddle.net/aohcqLvu/

JavaScript:
totalP.forEach(function (index) {
        players.add(".playSingle" + index, (playerVarsList[index] || {}));
    });

I receive this error:

Uncaught SyntaxError: Failed to execute 'querySelector' on 'Document': '.playSingle[object HTMLButtonElement</a>' is not a valid selector.
 
I was trying to show you that you shouldn't have extra parameters in a function if you are not using them if they serve no purpose to the function itself

I receive an error then:

 
Remove the brackets [ ] in the console.log

Lots of stuff comes up but I don't know what it all means.

https://jsfiddle.net/c63w71fz/2/

JavaScript:
totalP.forEach(function(index) {
    console.log(index);
    players.add(".playSingle" + index, (playerVarsList[index] || {}));
  });

Code:
[object HTMLButtonElement] {
  accessKey: "",
  addEventListener: function addEventListener() { [native code] },
  after: function after() { [native code] },
  animate: function animate() { [native code] },
  append: function append() { [native code] },
  appendChild: function appendChild() { [native code] },
  ariaAtomic: null,
  ariaAutoComplete: null,
  ariaBrailleLabel: null,
  ariaBrailleRoleDescription: null,
  ariaBusy: null,
  ariaChecked: null,
  ariaColCount: null,
  ariaColIndex: null,
  ariaColSpan: null,
  ariaCurrent: null,
  ariaDescription: null,
  ariaDisabled: null,
  ariaExpanded: null,
  ariaHasPopup: null,
  ariaHidden: null,
  ariaInvalid: null,
  ariaKeyShortcuts: null,
  ariaLabel: null,
  ariaLevel: null,
  ariaLive: null,
  ariaModal: null,
  ariaMultiLine: null,
  ariaMultiSelectable: null,
  ariaOrientation: null,
  ariaPlaceholder: null,
  ariaPosInSet: null,
  ariaPressed: null,
  ariaReadOnly: null,
  ariaRelevant: null,
  ariaRequired: null,
  ariaRoleDescription: null,
  ariaRowCount: null,
  ariaRowIndex: null,
  ariaRowSpan: null,
  ariaSelected: null,
  ariaSetSize: null,
  ariaSort: null,
  ariaValueMax: null,
  ariaValueMin: null,
  ariaValueNow: null,
  ariaValueText: null,
  assignedSlot: null,
  attachInternals: function attachInternals() { [native code] },
  attachShadow: function attachShadow() { [native code] },
  ATTRIBUTE_NODE: 2,
  attributes: [object NamedNodeMap] {
    0: [object Attr] { ... },
    1: [object Attr] { ... },
    2: [object Attr] { ... },
    3: [object Attr] { ... },
    getNamedItem: function getNamedItem() { [native code] },
    getNamedItemNS: function getNamedItemNS() { [native code] },
    item: function item() { [native code] },
    length: 4,
    removeNamedItem: function removeNamedItem() { [native code] },
    removeNamedItemNS: function removeNamedItemNS() { [native code] },
    setNamedItem: function setNamedItem() { [native code] },
    setNamedItemNS: function setNamedItemNS() { [native code] }
  },
  attributeStyleMap: [object StylePropertyMap] {
    append: function append() { [native code] },
    clear: function clear() { [native code] },
    delete: function delete() { [native code] },
    entries: function entries() { [native code] },
    forEach: function forEach() { [native code] },
    get: function get() { [native code] },
    getAll: function getAll() { [native code] },
    has: function has() { [native code] },
    keys: function keys() { [native code] },
    set: function set() { [native code] },
    size: 0,
    values: function values() { [n

What do I do next?
 
Last edited:
Lots of stuff comes up but I don't know what it all means.

https://jsfiddle.net/c63w71fz/2/

JavaScript:
totalP.forEach(function(index) {
    console.log(index);
    players.add(".playSingle" + index, (playerVarsList[index] || {}));
  });

Code:
[object HTMLButtonElement] {
  accessKey: "",
  addEventListener: function addEventListener() { [native code] },
  after: function after() { [native code] },
  animate: function animate() { [native code] },
  append: function append() { [native code] },
  appendChild: function appendChild() { [native code] },
  ariaAtomic: null,
  ariaAutoComplete: null,
  ariaBrailleLabel: null,
  ariaBrailleRoleDescription: null,
  ariaBusy: null,
  ariaChecked: null,
  ariaColCount: null,
  ariaColIndex: null,
  ariaColSpan: null,
  ariaCurrent: null,
  ariaDescription: null,
  ariaDisabled: null,
  ariaExpanded: null,
  ariaHasPopup: null,
  ariaHidden: null,
  ariaInvalid: null,
  ariaKeyShortcuts: null,
  ariaLabel: null,
  ariaLevel: null,
  ariaLive: null,
  ariaModal: null,
  ariaMultiLine: null,
  ariaMultiSelectable: null,
  ariaOrientation: null,
  ariaPlaceholder: null,
  ariaPosInSet: null,
  ariaPressed: null,
  ariaReadOnly: null,
  ariaRelevant: null,
  ariaRequired: null,
  ariaRoleDescription: null,
  ariaRowCount: null,
  ariaRowIndex: null,
  ariaRowSpan: null,
  ariaSelected: null,
  ariaSetSize: null,
  ariaSort: null,
  ariaValueMax: null,
  ariaValueMin: null,
  ariaValueNow: null,
  ariaValueText: null,
  assignedSlot: null,
  attachInternals: function attachInternals() { [native code] },
  attachShadow: function attachShadow() { [native code] },
  ATTRIBUTE_NODE: 2,
  attributes: [object NamedNodeMap] {
    0: [object Attr] { ... },
    1: [object Attr] { ... },
    2: [object Attr] { ... },
    3: [object Attr] { ... },
    getNamedItem: function getNamedItem() { [native code] },
    getNamedItemNS: function getNamedItemNS() { [native code] },
    item: function item() { [native code] },
    length: 4,
    removeNamedItem: function removeNamedItem() { [native code] },
    removeNamedItemNS: function removeNamedItemNS() { [native code] },
    setNamedItem: function setNamedItem() { [native code] },
    setNamedItemNS: function setNamedItemNS() { [native code] }
  },
  attributeStyleMap: [object StylePropertyMap] {
    append: function append() { [native code] },
    clear: function clear() { [native code] },
    delete: function delete() { [native code] },
    entries: function entries() { [native code] },
    forEach: function forEach() { [native code] },
    get: function get() { [native code] },
    getAll: function getAll() { [native code] },
    has: function has() { [native code] },
    keys: function keys() { [native code] },
    set: function set() { [native code] },
    size: 0,
    values: function values() { [n

What do I do next?
Ok, so now that we have that, I am assuming that you meant to index to be numerical, correct?
 
Yes, if that is the right recourse from here that will have this issue resolved, then changed to numerical would be correct.
So what you would be looking for looks something like this
JavaScript:
totalP.forEach(function(element, index) {
    players.add(".playSingle" + index, (playerVarsList[index] || {}));
  });

One thing to note about forEach function: your first parameter in the function will always be the current element being looked at. If you would like to get an index, then you need to pass in a secondary parameter like above. This is why having proper naming convention is quite helpful 🙂 . Yes, the "_" parameter you had originally, was indeed valid, for this reason.
 
So what you would be looking for looks something like this
JavaScript:
totalP.forEach(function(element, index) {
    players.add(".playSingle" + index, (playerVarsList[index] || {}));
  });

One thing to note about forEach function: your first parameter in the function will always be the current element being looked at. If you would like to get an index, then you need to pass in a secondary parameter like above. This is why having proper naming convention is quite helpful 🙂 . Yes, the "_" parameter you had originally, was indeed valid, for this reason.

What do I do after this?

This would be havening it changed to numerical?

JavaScript:
totalP.forEach(function(element, index) {
    players.add(".playSingle" + index, (playerVarsList[index] || {}));
  });
 
Now, let's look at what that call to add() function is doing
JavaScript:
[QUOTE]
players.add(".playSingle" + index, (playerVarsList[index] || {}));
[/QUOTE]
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom