How to get text inside a range and use it in another script
let's say ..
<span id="Singer">Bieber</span>
I know using the DOM I can duplicate the "Bieber" anywhere.
<script>
x=document.getElementById("Singer");document.write("<span>" + x.innerHTML + "</span>");</script>
.. but id doesn't work when I want to insert a DOM script in the script below. Is there a way to put "Bieber" in the script below?
<script language=JavaScript src="http://lyricfind.rotator.hadj7.adjuggler.net/servlet/ajrotator/275968/0/vj?z=lyricfind&dim=35518&click=&kw=<I WANT TO PUT BIEBER HERE>"></script>
+3
source to share
1 answer
You have a problem with the browser disabling the request for the script before it can access the span element. This can be done by dynamically inserting a script tag after reading the range:
window.onload = function(){
var x = document.getElementById("Singer");
var s = document.createElement("script");
s.src = "http://lyricfind.rotator.hadj7.adjuggler.net/servlet/ajrotator/275968/0/vj?z=lyricfind&dim=35518&click=&kw=" + encodeURIComponent(x.innerHTML);
s.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(s);
};
It assumes you have a section on your page <head>
. If not, you can add it to the body.
0
source to share