0

Everytime a new input is given, the array is somehow merged with the old and new values such that the slider displays all the videos from the different cases.

I tried refreshing the array using myUrlArray = []; after every switch case and also tried myUrlArray.length=0 but neither seems to work.

What have i done wrong? Also is there a way to pause the slider if a video is played? i tried using the youtube api <script src="https://www.youtube.com/iframe_api"></script> and fiddled with some code here and there but i can't seem to make that work either.

<input  id="inputIQ" placeholder="Search news. ." type="text" required list= "companylist" name="companyname"/>
  <datalist id="companylist">
    <option value="CNBC">
    <option value="BLOOMBERG">

<input type="submit" value="Submit" onclick="inputIQFunction(); ">
<iframe id="myiFrame" width="100%" height="470px" src="https://www.youtube.com/embed/AG8w0TEZunE?enablejsapi=1"></iframe>



function inputIQFunction() {

var x = document.getElementById("inputIQ").value;
var myUrlArray = [
    ];

switch (x) {
  case  "CNBC":
 myUrlArray =[];
myUrlArray.length = 0;
  myUrlArray = [
        "https://www.youtube.com/embed/y_ziBdLhQBY?enablejsapi=1", 
        "https://www.youtube.com/embed/y_ziBdLhQBY?enablejsapi=1"

    ];
 break;
  case "BLOOMBERG" :
myUrlArray =[];
myUrlArray.length = 0;
    myUrlArray = [
        "https://www.youtube.com/embed/X2LwIiKhczo?enablejsapi=1", 
        "https://www.youtube.com/embed/X2LwIiKhczo?enablejsapi=1"

    ];
 break;
 default:
    text = "No value found";

}

 var u = 1;

    setInterval(function(){
        document.getElementById('myiFrame').setAttribute('src',myUrlArray[u]);
        u++;
        if (u == myUrlArray.length) {u = 0;}
    },5000);
}

Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
Mahim Dhungel
  • 61
  • 1
  • 4
  • This will answer your question https://stackoverflow.com/a/2064863/1517029 and https://stackoverflow.com/a/2064883/1517029 – The_ehT Nov 11 '19 at 14:12
  • i made a function ````function refreshIframe() { var ifr = document.getElementsById('myiFrame'); ifr.src = ifr.src; }```` and called it on ```onclick="inputIQFunction(); refreshIframe(); "``` but that doesn't seem to work. Also i tried to put the codes you suggested inside the switch cases but that didn't help either. How should i proceed? – Mahim Dhungel Nov 12 '19 at 04:10

0 Answers0