Похожие чаты

How to change background colour of an element? It's throwing error

-
Uncaught TypeError: Cannot set properties of undefined (setting 'background')
Code -
function changeBg() { document.getElementsByClassName("about-opt").style.background = "#99f";
}

15 ответов

6 просмотров

getElementsByClassName returns a collection of elements, not a single element

𝔄𝔐𝔄𝔑 𝔎ℜ- Автор вопроса
𝔄𝔐𝔄𝔑 𝔎ℜ- Автор вопроса
𝔄𝔐𝔄𝔑 𝔎ℜ
Means?

Do a google search for- changeBg function javascript

Simple: Use loop.Why don't you use adding a class instead of directly applying the style?

function changeBackGround(){ //if is one element let element = document.querySelector(".about-opt"); if(element){ element.style.background = "#99f"; } //if is more thnan one element let elements = document.querySelectorAll(".about-opt"); for(let el of elements)[ el.style.background = "#99f"; ] }

Mr Thieves
function changeBackGround(){ //if is one eleme...

Don't spoon feed by sharing the whole code

Mr Thieves
why?

they won't learn anything

𝔄𝔐𝔄𝔑 𝔎ℜ- Автор вопроса
Mahdi
Simple: Use loop.Why don't you use adding a class ...

I want to change the background colour of elements using onclick of a button: Here is a sample - <button onclick="changeBg()"> This is js code - function changeBg() { document.getElementsByClassName("about-opt").style.background = "#99f"; }

𝔄𝔐𝔄𝔑 𝔎ℜ
I want to change the background colour of elements...

The proper way is to toggle a class on your body element, you css styles then match the body class and element

𝔄𝔐𝔄𝔑 𝔎ℜ- Автор вопроса
𝔄𝔐𝔄𝔑 𝔎ℜ
toggle a class by javascript?

Actually that's the most efficient way i can think of, without looping through every element

Похожие вопросы

Обсуждают сегодня

30 BCH videos in Hausa Language successfully delivered. In December 2023, I created a Flipstarter to produce Bitcoin Cash videos in the Hausa language for greater outreach an...
Munnir
5
читать файл максимально быстро? странный вопрос))
zamtmn
53
тоесть, указав return eax, сгенерируется никому ненужная инструкция mov eax,eax ?
Aiwan \ (•◡•) / _bot
24
Посоветуйте пожалуйста аудио плеер для библиотеки mp3- FLAC на компьютере. На Винде пользовался Aimp, в том числе для конвертации. Много нужных команд было прямо в контекст...
Виктор Кузнецов
31
Hi guys , do we have update on voting?
HB
15
How Much Btc Falling Down ?
Suriya
22
Why build it new? Why not try and get projects like fjord foundry etc. to launch? There is more power in getting real adoption and other teams on than to start everything a ne...
Qrayon Shinchan
11
ᴅᴏ ʏᴏᴜ ᴋɴᴏᴡ ᴛʜᴀᴛ ʏᴏᴜ ᴄᴀɴ ꜱᴇᴄᴜʀᴇ ᴀ ᴍᴀꜱꜱɪᴠᴇ ꜰɪɴᴀɴᴄɪᴀʟ ꜰᴜᴛᴜʀᴇ ʙʏ ᴊᴏɪɴɪɴɢ ᴛʜᴇ ᴏɴ ɢᴏɪɴɢ Qᴜᴀɴᴛᴜᴍ_ᴀɪ_ʀᴏʙᴏᴛ ᴛʀᴀᴅɪɴɢ ʙʏ ᴇʟᴏɴ ᴍᴜꜱᴋ? 👇👇👇JOIN NOW https://t.me/+3Ii7e3UQKRpiZjZk https://t...
Niklas Orlowski
3
It is definitely an interesting conversation. Breaking up the users into 2 personas: 1. Someone who wants to use the products (I want “x” pegged stablecoin that provides safe...
Bueno Pues
10
Hi guys ! Anvil is out ! Time for me to stake on capacity. Does someone has a clear tutorial ? I’m thinking about buying a ledger. Is that really possible to stake directl...
Mister Grega
16
Карта сайта