Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace img with iframe on click of button
    primarykey
    data
    text
    <p>Im pretty new to js/jquery and need some help from someone more knowledgeable!</p> <p>I have had a good look on this site and on the web but cannot find the answer myself. I've played around myself but with my limited knowledge I just cannot figure out what to do next.</p> <p><strong>What I've got</strong></p> <p>I have an <code>img</code> that when clicked is replaced by an <code>iframe</code> (youtube video), much like a placeholder/poster, and that works perfectly with the below code, which I originally found here: <a href="https://stackoverflow.com/questions/13725683/how-to-add-a-splash-screen-placeholder-image-for-a-youtube-video">How to add a splash screen/placeholder image for a YouTube video</a>:</p> <pre><code>&lt;div class="playbutton"&gt;&lt;/div&gt; &lt;img src="image.jpg" data-video="http://www.youtube.com/videolink"&gt; &lt;script&gt; $('img').click(function(){ var video = '&lt;div class="video-container"&gt;&lt;iframe src="'+ $(this).attr('data-video') +'"&gt;&lt;/iframe&gt;&lt;/div&gt;'; $(this).replaceWith(video); }); &lt;/script&gt; </code></pre> <p>As I said - it works perfectly when the image is clicked it <em>is</em> replaced by the video.</p> <p><strong>The Problem</strong></p> <p>I have now added a play button (currently a <code>div</code> but I can use <code>img</code> instead) floating above the <code>img</code>. If I click on the play button nothing happens (obviously as it is nothing to do with the script).</p> <p>I want this play button to "trigger" the replacing of the <code>img</code> with the <code>iframe</code> but I do not have a clue what to add to my script to make this happen.</p> <p><strong>What I'm looking for</strong></p> <p>I'm looking for the play button to be the "trigger" when clicked or ideally to have <strong>both</strong> the play button and the <code>img</code> as "triggers" so it works if a user clicks on the image too and not just the play button.</p> <p><strong>While I've got you...</strong></p> <p>Would I also be able to replace the <code>iframe</code> with the <code>img</code> again if a close button is added to the mix and clicked on?</p> <p>Any help will be greatly appreciated!</p> <p>Dale</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload