I am trying to get parameter value from the url, add it inside a div and then clone that value inside the img src attribute.
I can get the parameter value inside a div, and get it to clone as well but it's doing it like this:
<img id="target" src="images/">samplevalue.png</img>
I need to do like it this:
<img id="target" src="images/samplevalue.png" />
Sample parameter: param1=samplevalue.png
here's the code:
html
<div id="block1"></div>
<img id="target" src=""/>
jQuery/Javascript
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return decodeURIComponent(pair[1].replace(/\+/g, " "));
}
}
return decodeURIComponent(pair[1]);
}
document.getElementById("block1").innerHTML =
getQueryVariable("param1");
$(document).ready(function() {
$("img#target").attr("src", "images/").html($("#block1").html());
});
Even if adding the parameter value straight inside the img src attribute is fine too than adding first inside the div.
Any help on this is highly appreciated. Thank you very much in advance.