Reputation: 2340
I was working with an html page now i am trying to rewrite a div style to another style it'll look like this <div class="fs-stretcher" style="width: 1170px; height: 480px;"></div>
. for that i am using java script like this
<script>
window.onload=document.getElementsByClassName('fs-stretcher')[0].setAttribute("style","width: 0px; height: 0px; background-color: yellow;")
</script>
but it is not working somebody please help me to fix this.
Upvotes: 0
Views: 237
Reputation: 28588
window onload is a function, and you should need to bind a function to this event.
so try:
window.onload = function () {
document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
or you can create a function
function setElementOnLoad() {
document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
and bind it to onload of body tag:
<body onload="setElementOnLoad()">
Upvotes: 0
Reputation: 1453
Another way to place the script after you div.
<html>
<head>
</head>
<body>
<div class="fs-stretcher" style="width: 1170px; height: 480px;">adv</div>
<script>
window.onload = document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 100px; height: 100px; background-color: yellow;")
</script>
</body>
</html>
This will also work fine. But if you use window.onload =function(){-----}
is best way as answer above by Arun P johny.
Upvotes: 0
Reputation: 388436
You have to assign a function reference to window.onload
.
window.onload = function () {
document.getElementsByClassName('fs-stretcher')[0].setAttribute("style", "width: 0px; height: 0px; background-color: yellow;")
};
your code is executing the statement document.getElementsByClassName....
and assigns the value returned by setAttribute
to onload
Upvotes: 3