How to change the color of two elements using javascript?

user3612671

I am a newbie at javascript and I want change the color of elem2. But this doesn´t work:

    var elem = document.getElementById("test");
    var elem2 = document.getElementById("test2");

    var color = elem.style.backgroundColor;
    elem2.style.backgroundColor = color;

Any ideas? THX

DarkAjax

Try this:

var elem = document.getElementById('test');
var elem2 = document.getElementById('test2');    

var color = window.getComputedStyle(elem).getPropertyValue('background-color');
elem2.style.backgroundColor = color;

Sample JSFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Split array element in two elements using javascript

From Dev

How do I change the color of the MFMessageComposeViewController elements?

From Dev

match and change color of html elements using regex

From Dev

How to change the CSS for all elements with a common identifier using Javascript?

From Dev

Change Background color of input field using JavaScript

From Dev

How to change color of elements with angular.js

From Dev

Match height of two HTML elements using Javascript

From Dev

How to change order of elements in html using javascript

From Dev

how to change the text color when checking on elements

From Dev

How to permanently change the color of some elements?

From Dev

Change color of element onMouseOver using javascript

From Dev

Change text color of old date using javascript

From Dev

How to change the color of Kendo Grid elements

From Dev

How to validate email live using javascript and change textbox background color

From Dev

how to change background color of my two td's using jQuery?

From Dev

How to change the CSS for all elements with a common identifier using Javascript?

From Dev

How to change div background color using Javascript and Ajax?

From Dev

How can I change the background color of table cell (td) using two buttons for different colors?

From Dev

Background color change from dropdown using javascript

From Dev

how to change color of column in html table using javascript

From Dev

change number of elements color

From Dev

How to change the color of text between two positions?

From Dev

How to change button color in javascript?

From Dev

javascript using hover to change color

From Dev

How to toggle between two buttons and change color using angular

From Dev

how o change background color of html using javascript in chrome console

From Dev

How to change the color of particular characters of string using JavaScript?

From Dev

Change color from more elements then one in Javascript?

From Dev

How to change the words color in a text using regex - Javascript

Related Related

  1. 1

    Split array element in two elements using javascript

  2. 2

    How do I change the color of the MFMessageComposeViewController elements?

  3. 3

    match and change color of html elements using regex

  4. 4

    How to change the CSS for all elements with a common identifier using Javascript?

  5. 5

    Change Background color of input field using JavaScript

  6. 6

    How to change color of elements with angular.js

  7. 7

    Match height of two HTML elements using Javascript

  8. 8

    How to change order of elements in html using javascript

  9. 9

    how to change the text color when checking on elements

  10. 10

    How to permanently change the color of some elements?

  11. 11

    Change color of element onMouseOver using javascript

  12. 12

    Change text color of old date using javascript

  13. 13

    How to change the color of Kendo Grid elements

  14. 14

    How to validate email live using javascript and change textbox background color

  15. 15

    how to change background color of my two td's using jQuery?

  16. 16

    How to change the CSS for all elements with a common identifier using Javascript?

  17. 17

    How to change div background color using Javascript and Ajax?

  18. 18

    How can I change the background color of table cell (td) using two buttons for different colors?

  19. 19

    Background color change from dropdown using javascript

  20. 20

    how to change color of column in html table using javascript

  21. 21

    change number of elements color

  22. 22

    How to change the color of text between two positions?

  23. 23

    How to change button color in javascript?

  24. 24

    javascript using hover to change color

  25. 25

    How to toggle between two buttons and change color using angular

  26. 26

    how o change background color of html using javascript in chrome console

  27. 27

    How to change the color of particular characters of string using JavaScript?

  28. 28

    Change color from more elements then one in Javascript?

  29. 29

    How to change the words color in a text using regex - Javascript

HotTag

Archive