I have a J.script like below and i want to add alt tag dynamically to the image(id="consumer Header") based on some conditions.
I have tried below ways like by giving id to div and set attribute but nothing works for me(consumer header is null).
window.onload = getStrings();
function getStrings() { var $consumerheader = document.getElementById("consumerheader"); $consumerheader.setAttribute("alt", "R- Insurance"); var test = document.getElementById("consumerHeader").alt; alert("test"); //var $img = $divconsumerHeader.getElementsByTagName("img")[0]; //control.setAttribute("consumerheader","R- Insurance") // var test1 = document.getElementsByTagName("img")[0]; // alert("test1"); }
<div id="divconsumerHeader" class="head" style="text-align: center;"> <img src="shared/images/headers/ConsumerOnline.png" id="consumerHeader" alt="" style="width: 177" /> </div>
ID of the Image in your HTML is consumerHeader
and in Javascript you are using consumerheader
. that's why its not working.
function getStrings()
{
var $consumerheader = document.getElementById("consumerheader");
// ^ Here is the issue make this "h" to "H"
$consumerheader.setAttribute("alt", "R- Insurance");
var test = document.getElementById("consumerHeader").alt;
alert("test");
//var $img = $divconsumerHeader.getElementsByTagName("img")[0];
//control.setAttribute("consumerheader","R- Insurance")
// var test1 = document.getElementsByTagName("img")[0];
// alert("test1");
}
Note: Javascript is case sensitive .
Here is an working fiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.