+ 8

How can I change color when I click on button by using javascript?

html or js

20th Jan 2019, 8:15 AM
ArkarHtun
ArkarHtun - avatar
7 odpowiedzi
+ 7
HTML <button onclick=color()></button> JS function color(){ document.getElementmById("id of your element").style.color = "your color code" }; If you don't understand, please tell me, i will explain.
20th Jan 2019, 8:43 AM
Maneren
Maneren - avatar
+ 9
very thank for all answers
20th Jan 2019, 9:36 AM
ArkarHtun
ArkarHtun - avatar
+ 2
Simplest way is to do this online, but maybe you should use a function handle this. https://code.sololearn.com/W8s2OwM8xBJg/?ref=app
20th Jan 2019, 8:53 AM
Dennis Hain
Dennis Hain - avatar
+ 2
<button onclick="this.color='red'" >Change color</button> // this change button color to red when click.
20th Jan 2019, 9:01 AM
Calviղ
Calviղ - avatar
+ 2
Ok, thanks
20th Jan 2019, 10:13 AM
Maneren
Maneren - avatar
+ 1
Dennis Hain Do one have to write "javascript:" in onclick attribute? I use it without and it works.
20th Jan 2019, 8:57 AM
Maneren
Maneren - avatar
+ 1
I think there is no need to use this prefix any longer, but in the good old days it'd made sense.
20th Jan 2019, 9:01 AM
Dennis Hain
Dennis Hain - avatar