I have an HTML page which contains:
dropdown with 3 options with an onchange event to a JS functionThe other solution is great :) ...but if you still want to create elements via JavaScript you should use appendChild instead of innerHTML...
Here is what your JavaScript should be :)
<script type="text/javascript">
function getData(title)
{
switch(title)
{
case "ro":
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "js1.js";
s.innerHTML = null;
s.id = "widget";
document.getElementById("output").innerHTML = "";
document.getElementById("output").appendChild(s);
break;
case "en":
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "js2.js";
s.innerHTML = null;
s.id = "widget";
document.getElementById("output").innerHTML = "";
document.getElementById("output").appendChild(s);
break;
default:
void(0);
}
}
</script>
function getData(title){
document.getElementById("widget").src= (title == "ro" || title == "-") ? "js1.js" : "js2.js";
}
do simply:
function getData(title) {
switch(title) {
case "ro":
document.getElementById("output").innerHTML = '<script id="widget" type="text/javascript" src="js1.js" />';
break;
case "en":
document.getElementById("output").innerHTML = '<script id="widget" type="text/javascript" src="js2.js" />';
break;
default:
void(0);
}
}