+ 11

How can I change the background-color of <td> whenever I click on that?

9th Dec 2019, 6:12 AM
Shreyansh
Shreyansh - avatar
17 odpowiedzi
+ 10
[...document.getElementsByTagName("td")].forEach(td => td.addEventListener("click", e => e.target.style.background= "red")) https://code.sololearn.com/WV806eog003Y/?ref=app
9th Dec 2019, 6:48 AM
Calviղ
Calviղ - avatar
+ 9
Calviղ nice bro but could you please simplify the code I am having trouble in understanding jquery code
9th Dec 2019, 12:03 PM
Shreyansh
Shreyansh - avatar
+ 7
<td onclick="colour ()" id="td"> Some text </td> <script> function colour (){ document.getElementById('td').style. backgroundColor='red';} </script>
9th Dec 2019, 6:27 AM
Kashyap Kumar
Kashyap Kumar - avatar
+ 7
Calviղ I mean ECMAScript6
9th Dec 2019, 5:12 PM
Shreyansh
Shreyansh - avatar
+ 7
marjel101 as you wish
9th Dec 2019, 5:16 PM
Shreyansh
Shreyansh - avatar
+ 6
on Click change style :color=red
10th Dec 2019, 8:59 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
Shreyansh It is not jQuery codes, it's javascript code. Please restudy your javascript course.
9th Dec 2019, 3:50 PM
Calviղ
Calviղ - avatar
+ 3
Shreyansh The code of Calviղ uses arrow functions, which I am also not very used to. Maybe you understand it better in this form: https://code.sololearn.com/WnNb5I2oi8Yl/?ref=app
9th Dec 2019, 5:09 PM
marjel101
marjel101 - avatar
+ 3
Shreyansh You dont need to mark my answer as best, it’s still Calviղ ‘s code 😶
9th Dec 2019, 5:14 PM
marjel101
marjel101 - avatar
+ 3
marjel101 Thanks for showing the simple way of the javascript code.
10th Dec 2019, 2:09 AM
Calviղ
Calviղ - avatar
+ 3
$(function(){ $("td").on("click", function(){ $(this).css({"color": "red"}); }); });
11th Dec 2019, 2:39 AM
Raj kumar Singh
Raj kumar Singh - avatar
+ 2
CSS: td:hover { background-color: red; }
9th Dec 2019, 9:58 PM
Ilinka Panovska
+ 2
td:focus{ background : red; }
10th Dec 2019, 5:11 PM
Gurpreet Kaur
Gurpreet Kaur - avatar
+ 1
use js DOM
10th Dec 2019, 6:58 PM
Elias Abdurahman Yasin
Elias Abdurahman Yasin - avatar
+ 1
use js DOM
10th Dec 2019, 6:58 PM
Elias Abdurahman Yasin
Elias Abdurahman Yasin - avatar
0
td color
9th Dec 2019, 10:41 AM
Moaz Ahmed
Moaz Ahmed - avatar
9th Dec 2019, 2:44 PM
Geek
Geek - avatar