Replacing modal with 4 modal buttons that open up to 4 videos

Is that what I would be doing here?

I don’t know how difficult this is going to be.

Click on a button, it opens into a video, click close button, click on a different button, opens into a video.

Starting point: https://jsfiddle.net/j759xac4/1/

Example: https://getbootstrap.com/docs/4.0/components/modal/


For this to work, what is the logic I need to know?

My thinking would be this:

(function manageButtonA() {
}());

(function manageButtonB() {
}());

(function manageButtonC() {
}());

(function manageButtonD() {

  function handleModal(modalId) {
    const modal = document.getElementById(modalId);
    modal.classList.remove("open");
  }

  function hideContainer(containerSelector) {
    const container = document.querySelector(containerSelector);
    container.classList.add("hide");
  }


  function resetPage() {
    handleModal("myModal");
  }

  function exitClickHandler() {
    videoPlayer.players.forEach(function(player) {
      player.destroy();
    });
    resetPage();

    // Initialize the video when the button is clicked
    loadPlayer.add(".buttonD", {
      videoId: "CHahce95B1g"
    });

    console.log("hit");

  }
  const exitButton = document.querySelector(".exit");
  exitButton.addEventListener("click", exitClickHandler);

}());

In the code here: https://jsfiddle.net/j759xac4/1/

I would need to hide the modal where I would then show the buttons on the page.

The buttons would be rectangle with text written on each.

I’m confused on how this would be done.

If I was able to get one button to pop up a video and close, I would be able to figure out the others.

3 or 4 modal buttons where a single video would pop up where you would close it after.

The exit button at the bottom would take you to the sliding panels that open up into the videos.


I was thinking I could use the logic from here: https://jsfiddle.net/wj548m6g/1/

Maybe something like this:

<div class="container1 hide">
<div id="myModalA" class="modal open hide">
	<div class="modal-content">
		<button class="exitInitial" type="button" title="Exit" aria-label="Close"></button>
		<div class="ratio-keeper">
			<div class="wrap ">
				<div class="video video-frame" data-id="PL6KcpeEiGPpxSF3yIJSsNSOZGb4hBXhrN"></div>
			</div>
			<div class="panel">
			  
			</div>
		</div>
		<div class="playInitialA"></div>
  
	</div>

</div>
</div>


<div class="container2 hide">
<div id="myModalB" class="modal open">
	<div class="modal-content">
		<button class="exitInitial" type="button" title="Exit" aria-label="Close"></button>
		<div class="ratio-keeper">
			<div class="wrap ">
				<div class="video video-frame" data-id="PL6KcpeEiGPpxSF3yIJSsNSOZGb4hBXhrN"></div>
			</div>
			<div class="panel">
			  
			</div>
		</div>
		<div class="playInitialB"></div>
  
	</div>
</div>
</div>


<div class="container3 hide">
<div id="myModalC" class="modal open">
	<div class="modal-content">
		<button class="exitInitial" type="button" title="Exit" aria-label="Close"></button>
		<div class="ratio-keeper">
			<div class="wrap ">
				<div class="video video-frame" data-id="PL6KcpeEiGPpxSF3yIJSsNSOZGb4hBXhrN"></div>
			</div>
			<div class="panel">
			  
			</div>
		</div>
		<div class="playInitialC"></div>
  
	</div>
</div>
</div>



 <div class="button-container">
<button class="bc1" type="button"></button>
<button class="bc1 bc2" type="button"></button>
<button class="bc1 bc3" type="button"></button>
</div>
<!-- end modal -->

Is this the sort of thing you are looking for?

Yes!

I just made this: https://jsfiddle.net/mp7k3uyt/


That screen there with the buttons would replace the modal that is currently showing on the screen here.

https://jsfiddle.net/j759xac4/1/

Maybe it would need to be set up like this:

 <div class="button-container">
<button class="bc1" type="button"></button>
<button class="bc1 bc2" type="button"></button>
<button class="bc1 bc3" type="button"></button>
<button class="exitInitial" type="button" title="Exit" aria-label="Close"></button>
</div>

The buttons would activate the modal like this:

Its up to you to decide how the modal should look.

1 Like

Here is an example:

You click on a button, video opens, then you can close it.

You have everything you need to achieve that if you follow my example. All you have to do is put your video in my modal code.

I have this right now where the only thing working is the exit button:

https://jsfiddle.net/y9wrpfzk/

<div id="myModal" class="modal open">
  <div class="modal-content">

    <div class="buttonContainer">
      <button data-destination="#ba" class="playButton">ButtonA</button>
      <button data-destination="#bb" class="playButton">ButtonB</button>
      <button data-destination="#bc" class="playButton">ButtonC</button>
      <button class="exitInitial" type="button" title="Exit" aria-label="Close"></button>
    </div>

  </div>
  <div class="playInitial remove"></div>


  <div id="ba" class="modal">
    <div class="inner-modal">
      <h2>Modal 1</h2>
      <button class="close">&times</button>
    </div>
  </div>

  <div id="bb" class="modal">
    <div class="inner-modal">
      <h2>Modal 2</h2>
      <button class="close">&times</button>
    </div>
  </div>

  <div id="bc" class="modal">
    <div class="inner-modal">
      <h2>Modal 3</h2>

      <button class="close">&times</button>
    </div>
  </div>
</div>

Am I doing this right or half right?

Is the html almost ok?

now I have this: https://jsfiddle.net/dqna7ogb/4/

no errors, but still not working.

    <div class="buttonContainer">
      <button data-destination="#ba" class="playButton buttonA">ButtonA</button>
      <button data-destination="#bb" class="playButton buttonB">ButtonB</button>
      <button data-destination="#bc" class="playButton buttonC">ButtonC</button>
      <button class="exitInitial" type="button" title="Exit" aria-label="Close"></button>
    </div>
(function manageButtonA() {

  function resetPage() {
      // Destroy the first player if it exists
    if (videoPlayer.players[0]) {
      videoPlayer.players[0].destroy();
    }
    //handleModal("buttonA");
  }

  function modalClickHandler() {

    resetPage();

    loadPlayer.add(".buttonA", {
      videoId: "CHahce95B1g"
    });
   }
   
  const closeModal = document.querySelector(".close");
  closeModal.addEventListener("click", modalClickHandler);
  
  /*const openModal = document.querySelector(".buttonA");
  openModal.addEventListener("click", modalClickHandler);*/
}());

(function manageButtonB() {

  function resetPage() {
      // Destroy the first player if it exists
    if (videoPlayer.players[0]) {
      videoPlayer.players[0].destroy();
    }
    //handleModal("buttonA");
  }

  function modalClickHandler() {

    resetPage();

    loadPlayer.add(".buttonB", {
      videoId: "CHahce95B1g"
    });
   }
   
  const closeModal = document.querySelector(".close");
  closeModal.addEventListener("click", modalClickHandler);
  
  /*const openModal = document.querySelector(".buttonA");
  openModal.addEventListener("click", modalClickHandler);*/
}());

(function manageButtonC() {

  function resetPage() {
      // Destroy the first player if it exists
    if (videoPlayer.players[0]) {
      videoPlayer.players[0].destroy();
    }
    //handleModal("buttonA");
  }

  function modalClickHandler() {

    resetPage();

    loadPlayer.add(".buttonC", {
      videoId: "CHahce95B1g"
    });
   }
   
  const closeModal = document.querySelector(".close");
  closeModal.addEventListener("click", modalClickHandler);
  
  /*const openModal = document.querySelector(".buttonA");
  openModal.addEventListener("click", modalClickHandler);*/
}());

Maybe I should be using data-target="" instead of data-destination="", as that is used in the example that is given?

The codepen example that I shared uses data-target="" also.

What if data-target="" is used instead of data-destination="" ?

From AI:

The data-target and data-destination are both custom data attributes in HTML, often used in conjunction with JavaScript or libraries like Bootstrap. However, data-target is a more commonly used attribute, especially in Bootstrap, while data-destination is not a standard attribute and may be used in specific cases or libraries.

The data-target attribute in Bootstrap is used to make your life easier by not needing to write a single line of JavaScript to use their pre-made JavaScript components1. The data-target attribute should contain a CSS selector that points to the HTML Element that will be changed1. For example, in a modal:

HTML

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  [...]
</div>

In this example, the button has data-target="#myModal", if you click on it, <div id="myModal">… will be modified (in this case faded in).

On the other hand, data-destination is not a standard attribute and its behavior would depend on the specific JavaScript code or library that is using it. It might be used similarly to data-target, to specify a particular element or location in the HTML document, but without more context, it’s hard to say exactly how it’s used. You would need to refer to the documentation or code that is using this attribute for more specific information.

Remember, any attribute with the “data-” prefix is used to store custom data that won’t be rendered in the browser. So, data-destination could be used to store any custom data as per the requirements of your web application or the library you are using.

Makes absolutely no difference (assuming you aren’t using bootstrap) as they are both made up things. You can see my code works perfectly so it is your implementation that is wrong!

How do I have the implementation wrong?

What part?

Is the html wrong?

What needs to be fixed?

Is this setup fine?

(function manageButtonA() {

  function resetPage() {
      // Destroy the first player if it exists
    if (videoPlayer.players[0]) {
      videoPlayer.players[0].destroy();
    }
    //handleModal("buttonA");
  }

  function modalClickHandler() {

    resetPage();

    loadPlayer.add(".buttonA", {
      videoId: "CHahce95B1g"
    });
   }
   
  const closeModal = document.querySelector(".close");
  closeModal.addEventListener("click", modalClickHandler);
 
}());

Where is the js I gave you in that code?

There are numerous close buttons but you only target one with your js.

When you do things like repeating enormous chunks of code like this:

(function manageButtonA() {

(function manageButtonB() {

(function manageButtonC() {

(function manageButtonD() {

Then you are doing it wrong as they all have basically the same code and the only difference is the video id. You could do it in one function and supply the video based on which button was clicked. You don’t need to define the same function for multiple buttons as that is not manageable.

Look at the code I gave you. It handles all the modals from one function including opening and closing.

You need to put your video code inside my function and apply the correct video by checking the data-destination as required.

I would need multiple so that they don’t all launch at the same time.

No?

Isn’t code separation/isolation a good thing?

So it’s not all jumbled together.

Would I be able to get it to work both ways to see the difference?

function modalClickHandler() {
  resetPage();
  // Initialize the player when the modal is clicked
  loadPlayer.add(".buttonA", {
    videoId: "CHahce95B1g"
  });

}

No

Not when you are doing exactly the same thing to multiple elements apart from one line of code.

You are trying to do too much at once as I keep saying.

This is a new layout so build it with minimum code first as per my example. Then add the videos into the reduced code and get them working first. Once that is done then you can see about implementing that with your other functions. Work on one thing at a time…

In your last example you don’t even add the click handler to the buttons so I don’t know how you expect them to open. You also add multiple event handlers to only the first ‘close’ item.

Start small and get the videos working first even if you just add them manually to start with. Then once working you can work on a plan to automate them. There’s just too much going on in your code to try and do everything all at once.

1 Like

I thought I would be just hiding and un-hiding, like how the other ones work.

Because 2 clicks are needed, not one.

So, then, like this then?

Would you be able to show me one video working to get a general idea on how to do it?

I am just trying to have one button show a video, after that I would probably be able to figure out the rest.

function resetPage() {
      // Destroy the first player if it exists
    if (videoPlayer.players[0]) {
      videoPlayer.players[0].destroy();
    }
    //handleModal("buttonA");
  }

  function modalClickHandler() {

    resetPage();

    loadPlayer.add(".buttonA", {
      videoId: "CHahce95B1g"
    });
   }
   
  const closeModal = document.querySelector(".close");
  closeModal.addEventListener("click", modalClickHandler);
  
  const openModal = document.querySelector(".buttonA");
  openModal.addEventListener("click", modalClickHandler);
}());

You are doing too many things again.

Just work on opening the modal first. Forget about the players.

Just make the modal work.

This won’t work:

const closeModal = document.querySelector(".close");

There are multiple close buttons. If you want to do one at a time then you’d need the id.

e.g.

const closeModal = document.querySelector("'ba .close");

You; would need the same to open the modal and of course do it for all the different modals which is why I say that’s a bad approach. You aren’t using the data-destination targets I gave you so you don’t identify the modal.

Your players code is too complicated for me as it just jumps backwards and forwards and repeats itself. I can’t follow the logic.

You need a js guru not me unfrotunately :slight_smile:

1 Like

I just did this: https://jsfiddle.net/tgde4vz5/2/

var buttons = document.querySelectorAll('.playButton');
buttons.forEach(function(button) {
    button.addEventListener('click', function() {
        var destination = this.getAttribute('data-destination');
        var targetElement = document.querySelector(destination);
        // Perform your action with the targetElement here
        // For example, you might want to display the element
        targetElement.style.display = 'block';
    });
});

Wouldn’t one of these need to be placed inside a button?

        <div class="video video-frame" data-id="AxLxnN6z0Og"></div>
      </div>
      <div class="buttonA"></div>

Here is your code with only the video player code in it: https://jsfiddle.net/wj1xcv85/2/

I don’t know how to add this to it.

   function modalClickHandler() {
      // Destroy the first player if it exists
      if (videoPlayer.players[0]) {
        videoPlayer.players[0].destroy();
      }
      resetPage();

      loadPlayer.add(".buttonA", {
        videoId: "CHahce95B1g"
      });
    }

And this would still need to be added:

       <div class="video video-frame" data-id="AxLxnN6z0Og"></div>
      </div>
      <div class="buttonA"></div>

That would be:

added to this:

<div id="ba" class="modal">
  <div class="inner-modal">
    <h2>Modal 1</h2>
    <button class="close">&times</button>
  </div>
</div>

or this:

<button data-destination="#ba" class="playButton">ButtonA</button>