How to change the css of a dynamically added element?

daktau

I am adding several divs with class "data_record" dynamically to the DOM.

I want each one to turn a colour when clicked and for the others to turn back to a white background.

Here is my code to do this after having successfully added the data_record elements...

$(document).on('click', ".data_record", function(event) {
    //turn all others white (this DOES NOT work!)
    $('.data_record').css('background','#ffffff');

    //turn the clicked element a different colour (this DOES work!)
    $(this).css('background', '#EDC580');
});

So how to I target dynamically added elements by their class?

Thanks.

Milind Anantwar

try using .css('background-color',value) to set background color:

 $(document).on('click', ".data_record", function(event) {
//turn all others white (this now works!)
    $('.data_record').css('background-color','#ffffff');

//turn the clicked element a different colour (this DOES work!)
    $(this).css('background-color', '#EDC580');
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change attribute of dynamically added element on just calling a function

From Dev

change name of dynamically added select option element

From Dev

change name of dynamically added select option element

From Dev

How to change element dynamically?

From Dev

how to use jquery .data with dynamically added element

From Dev

How to remove an element that is added dynamically in Javascript

From Dev

How to slice text dynamically added to an element?

From Dev

How to dynamically change element alignment?

From Dev

How to dynamically change element alignment?

From Dev

Change onclick handler of the element added dynamically after the page has loaded

From Dev

Dynamically added element will not delete

From Dev

Adding CSS to jQuery dynamically added element inside of Polymer Component

From Dev

How to get element by id after id dynamically added to element?

From Dev

How to Apply CSS to element added by Javascript

From Dev

How to associate dynamically added input fields to parent element/id?

From Dev

How to bind a Jquery scroll event to a dynamically added element?

From Dev

How can I detect when an element dynamically added has loaded?

From Dev

How to add mouseenter event for dynamically added element with native JS

From Dev

How to find dynamically added element by data attribute using jQuery?

From Dev

jQuery handler - how to apply an action to dynamically added element

From Dev

How can I detect when an element dynamically added has loaded?

From Dev

How do I remove a dynamically added element after 5 seconds?

From Dev

how to know the element that performed the click in a checkbox list dynamically added (javascript)

From Dev

How Can I Animate Background Color Dynamically Added Element?

From Dev

How to make the DOM element stay(after refresh), which was added dynamically

From Dev

how to change text dynamically added button c#

From Dev

How to change the opacity on an element dynamically using javascript

From Dev

Add class to dynamically added element

From Dev

JQuery Selector on Dynamically Added Element

Related Related

  1. 1

    How to change attribute of dynamically added element on just calling a function

  2. 2

    change name of dynamically added select option element

  3. 3

    change name of dynamically added select option element

  4. 4

    How to change element dynamically?

  5. 5

    how to use jquery .data with dynamically added element

  6. 6

    How to remove an element that is added dynamically in Javascript

  7. 7

    How to slice text dynamically added to an element?

  8. 8

    How to dynamically change element alignment?

  9. 9

    How to dynamically change element alignment?

  10. 10

    Change onclick handler of the element added dynamically after the page has loaded

  11. 11

    Dynamically added element will not delete

  12. 12

    Adding CSS to jQuery dynamically added element inside of Polymer Component

  13. 13

    How to get element by id after id dynamically added to element?

  14. 14

    How to Apply CSS to element added by Javascript

  15. 15

    How to associate dynamically added input fields to parent element/id?

  16. 16

    How to bind a Jquery scroll event to a dynamically added element?

  17. 17

    How can I detect when an element dynamically added has loaded?

  18. 18

    How to add mouseenter event for dynamically added element with native JS

  19. 19

    How to find dynamically added element by data attribute using jQuery?

  20. 20

    jQuery handler - how to apply an action to dynamically added element

  21. 21

    How can I detect when an element dynamically added has loaded?

  22. 22

    How do I remove a dynamically added element after 5 seconds?

  23. 23

    how to know the element that performed the click in a checkbox list dynamically added (javascript)

  24. 24

    How Can I Animate Background Color Dynamically Added Element?

  25. 25

    How to make the DOM element stay(after refresh), which was added dynamically

  26. 26

    how to change text dynamically added button c#

  27. 27

    How to change the opacity on an element dynamically using javascript

  28. 28

    Add class to dynamically added element

  29. 29

    JQuery Selector on Dynamically Added Element

HotTag

Archive