3

I'm trying to change the color of the h3 within my div .card that was dynamically created, however, when I reload the page, it sets its values to default.

This is how I'm trying to change the color where color in parameter is the desired color to apply to the h3.

On button click, I'm creating the card:

function createCard(id, title, ...) {
    // Creates a main card div
    var $cardDiv = $('<div>', {
        class: 'col-md-12 card',
        "card-id": id
    });

    // h3 tag with title of note
    var $title = $('<h3>', {
        "data-toggle": 'modal',
        "data-target": '#update',
        click: function() {
            updateModal(id, title, note);
        }
    }).text(title);

    // Append to card
    $cardDiv.append($title);
}

After this, I'm calling the cardScheme method:

cardScheme('#29ABDA');

function cardScheme(color) {
    $('.card h3').css('color', color);
}

I realized that JavaScript/jQuery are unable to find the .card class since those cards were created dynamically.

var cards = document.getElementsByClassName('card');
for (var i in cards) {
    console.log('cards', cards[i]);
}
// returned {cards, 0}

How can I change the color of the h3?

6
  • you need to do this after the creation of DOM of div .card Commented Nov 17, 2016 at 7:22
  • so show where you are creating the divs dynamically, so that we can help Commented Nov 17, 2016 at 7:23
  • Define "dynamically created". Loaded from server? Created on clientside? ...? Commented Nov 17, 2016 at 7:23
  • where is you h3? add your html template Commented Nov 17, 2016 at 7:23
  • @SunilBN I am changing the color after the creation of the div and it shows up in the DOM as well. Commented Nov 17, 2016 at 7:29

3 Answers 3

2

You can call the cardScheme('#29ABDA'); inside the createCard method as below.

Note: I can't see where you are appending the dynamic div $cardDiv to the document, I mean to another existing div or body.

Working snippet:

function cardScheme(color) {
    $('.card h3').css('color', color);
}

function createCard(id, title, note) {
        
    // Creates a main card div
        var $cardDiv = $('<div>', {
            class: 'col-md-12 card',
            "card-id": id
        });
    
        // h3 tag with title of note
        var $title = $('<h3>', {
            "data-toggle": 'modal',
            "data-target": '#update',
            click: function() {
                updateModal(id, title, note);
            }
        }).text(title);
    
        // Append to card
        $cardDiv.append($title);
  
        $('#container').append($cardDiv);
    
        cardScheme('#29ABDA');
}

setTimeout(function(){
  createCard('test', 'testTitle', 'test note');
}, 2000);  // after 2 seconds
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting because I didn't have to call it inside that previously, but I guess that works. Thanks for the help!
0

You should add your JS code once the DOM is being loaded and ready to use. Like this -

$(document).ready(function(){
    $('.card h3').css('color', color);
});

4 Comments

what if he is creating div dynamically in client side
Still he should be able to access the dynamically created divs (Not on the fly created) once the DOM is ready. Right?
OP is creating on the fly as per question.
But it shows up in the DOM when I inspect elements so how would that be on fly again?
0
function createCard(id, title, ...) {
      ...
     // append this below the initial code you've
    //.ready make sures that function within is executed only when the specified HTML element is created in DOM
    $($title).ready(function(){
         //Then call your function
        cardScheme('#29ABDA');
    });
}

3 Comments

Can you explain your answer? What have you done and why is it working?
Explained in comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.