0

In the example below only one instance of substring (~code) is replaced by a variable value.

How to replace the same inside the iframe tag, i.e. embed/~code should be embed/92xR2fjbeUE

let file = $('#store_vid').html();
let code = '92xR2fjbeUE';
file = file.replace('~code', code);
console.log(file);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='store' id='store_vid'>
<div class = 'vidwrap' data-code='~code'>
<iframe src='https://www.youtube.com/embed/~code?rel=0&fs=0&modestbranding=1&showinfo=0'></iframe>
</div></div>
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • Does this answer your question? [How to replace all occurrences of a string?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string) – 3limin4t0r Apr 20 '20 at 13:19
  • @3limin4t0r - I see thanks. I'm surprised - that's such a minor task and I must study `63` answers. Not good for the reputation of this site. – qadenza Apr 20 '20 at 13:38
  • That's not the fault of SO that such a widely sought after question isn't implemented by the language itself. You also don't have to look at all the answers. If you find that there are too many I suggest looking only at the top rated few. – 3limin4t0r Apr 20 '20 at 15:09

2 Answers2

2

what I get by your question is, you want to replace ~code with your variable , and ~code is attributed to a div of yours. if you don't want to change the iframe src ~code then don't use code in if condition.

`<div class = 'vidwrap' data-code='~code'>`

document.addEventListener('DOMContentLoaded', (event) => {
  var code = '92xR2fjbeUE';
  document.getElementById("vidwrap").setAttribute("data-code", code);
  let iframeAttrSrc = document.getElementById("iframe").src;
  
  if(iframeAttrSrc.includes('~code')){
    alert("true");
    var newSRC = iframeAttrSrc.replace("~code", code);
    document.getElementById("iframe").setAttribute("src", newSRC);
  }
  

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='store' id='store_vid'>
<div class='vidwrap' data-code='~code' id="vidwrap">
<iframe id="iframe" src='https://www.youtube.com/embed/~code?rel=0&fs=0&modestbranding=1&showinfo=0'></iframe>
</div></div>
Atul Rajput
  • 4,073
  • 2
  • 12
  • 24
1

By default js try to replace the first instance of substring only. You can extend the string type like this

String.prototype.replaceAll = function (search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

Use the above method instead of replace

'12sdasdsadasd12asdasda12asdad'.replaceAll('12','89')//returns 89sdasdsadasd89asdasda89asdad

In your example

    String.prototype.replaceAll = function (search, replacement) {
        var target = this;
        return target.replace(new RegExp(search, 'g'), replacement);
    };

    let file = $('#store_vid').html();
    let code = '92xR2fjbeUE';
    file = file.replaceAll('~code', code);
    console.log(file);
Beingnin
  • 2,288
  • 1
  • 21
  • 37