I first wanted only to change the background of one element but then this came across:
TypeError: $(...).style is undefined
(in the Firefox Console)
HTML:
<!DOCTYPE html>
<html lang="DE">
<head>
<meta charset="utf-8"/>
<title>Laura Sack - Offizielle Webseite</title>
</head>
<body>
<div id="gallery-container" class="gallery-container cf"></div>
<script src="js/jquery.js"></script>
<script src="js/main.js"></script>
</body>
</html>
Javascript:
$(document).ready(function(){
$("#gallery-container").style.background = "black";
});
I first wanted only to change the background of one element but then this came across:
TypeError: $(...).style is undefined
(in the Firefox Console)
HTML:
<!DOCTYPE html>
<html lang="DE">
<head>
<meta charset="utf-8"/>
<title>Laura Sack - Offizielle Webseite</title>
</head>
<body>
<div id="gallery-container" class="gallery-container cf"></div>
<script src="js/jquery.js"></script>
<script src="js/main.js"></script>
</body>
</html>
Javascript:
$(document).ready(function(){
$("#gallery-container").style.background = "black";
});
Share
Improve this question
edited Jun 30, 2022 at 10:45
Patrick Yoder
1,1535 gold badges16 silver badges22 bronze badges
asked May 24, 2014 at 19:39
SteveSteve
1431 gold badge1 silver badge6 bronze badges
4
|
4 Answers
Reset to default 13you are mixing javascript with jquery.
In jquery you have to use css() to make it work like this:
$("#gallery-container").css("background","black");
Try this,
$(document).ready(function(){
$("#gallery-container").css('background-color','black');
});
This is the correct jQuery method:
$(document).ready(function(){
$("#gallery-container").css('background-color','black');
});
You need the existence by the following:
$(document).ready(function(){
let g = $("#gallery-container");
if(typeof g !== undefined) g.css('background','black');
});
style
is a property of a pure JS element object not of a jQuery object, try$("#gallery-container")[0].style
instead. However you should use the.css
jQuery method and forget about thestyle
property. – King King Commented May 24, 2014 at 19:41$("#gallery-container")
is a jQuery object and you are trying to style a dom node with your script, Either turn it to a dom node with including[0]
to it like$("#gallery-container")[0]
or as suggested use the jQuery's.css()
method to style it. – Jai Commented May 24, 2014 at 19:47[0]
was missing. dustindiaz.com/seven-togglers under "toggling multiple objects" – Marcos Commented Jan 12, 2015 at 13:54