Add class to dynamically added element

Jon

I have a shared left menu in a separate file from my pages so that I can update it in one place.

I am loading it onto the page in a .js file like so:

$(function () {
    $("#left-nav").load("templates/left-nav.html", function() {
        ...
    });

    ...
}

I would like to be able to set the nav-list item for the page to be selected. Something like this:

$("#calendar").addClass("selected");

...where #calendar is the ID of a list item in my left-nav.html template file.

Everything I've read says that I have to call addClass as a callback after I load in the file, but the following doesn't seem to work:

$(function () {
    $("#left-nav").load("templates/left-nav.html", function() {
        $("#calendar").addClass("selected");
    });

    ...
}

Question: Is it possible to add classes to dynamically added content on a page, and if so, how should I do it? Thanks!

Shriike

The code is correct as written, adduming that left-nav.html does contain an element with the ID of calendar.

The completed event is fired after the HTML is added to the DOM according to the jQuery documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add class to dynamically added elements

From Dev

jQuery select element with class added dynamically

From Dev

Click not recognized on element with dynamically added class

From Dev

How to add events for dynamically added single element which shares the class names in JQuery?

From Dev

How to add class to new dynamically added content?

From Dev

Add Event Listner to dynamically added element using JQuery?

From Dev

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

From Dev

Add Event Listner to dynamically added element using JQuery?

From Dev

Dynamically added element will not delete

From Dev

How to add CSS class dynamically to html element

From Dev

jQuery: Dynamically add class that belongs to another element

From Dev

Add class to an element, only when another class is added

From Dev

Watch for Dynamically Added Class

From Dev

Watch for Dynamically Added Class

From Dev

Add data attribute to element and dynamically add value from class name

From Dev

Add data attribute to element and dynamically add value from class name

From Dev

Class not being added to element

From Dev

JQuery Selector on Dynamically Added Element

From Dev

find index of dynamically added element

From Dev

why element is not added in dynamically in angular

From Dev

jQuery click on a dynamically added element

From Dev

Check if dynamically added element is ready

From Dev

Cannot remove a dynamically added element

From Dev

add color dynamically to added text

From Dev

Jquery Dynamically added class Not working

From Dev

Jquery Dynamically added class Not working

From Dev

Adding Class to Dynamically added textbox

From Dev

CSS Styling didn't get applied when class added dynamically for the element through jQuery

From Dev

Dynamically add element to layout

Related Related

  1. 1

    Add class to dynamically added elements

  2. 2

    jQuery select element with class added dynamically

  3. 3

    Click not recognized on element with dynamically added class

  4. 4

    How to add events for dynamically added single element which shares the class names in JQuery?

  5. 5

    How to add class to new dynamically added content?

  6. 6

    Add Event Listner to dynamically added element using JQuery?

  7. 7

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

  8. 8

    Add Event Listner to dynamically added element using JQuery?

  9. 9

    Dynamically added element will not delete

  10. 10

    How to add CSS class dynamically to html element

  11. 11

    jQuery: Dynamically add class that belongs to another element

  12. 12

    Add class to an element, only when another class is added

  13. 13

    Watch for Dynamically Added Class

  14. 14

    Watch for Dynamically Added Class

  15. 15

    Add data attribute to element and dynamically add value from class name

  16. 16

    Add data attribute to element and dynamically add value from class name

  17. 17

    Class not being added to element

  18. 18

    JQuery Selector on Dynamically Added Element

  19. 19

    find index of dynamically added element

  20. 20

    why element is not added in dynamically in angular

  21. 21

    jQuery click on a dynamically added element

  22. 22

    Check if dynamically added element is ready

  23. 23

    Cannot remove a dynamically added element

  24. 24

    add color dynamically to added text

  25. 25

    Jquery Dynamically added class Not working

  26. 26

    Jquery Dynamically added class Not working

  27. 27

    Adding Class to Dynamically added textbox

  28. 28

    CSS Styling didn't get applied when class added dynamically for the element through jQuery

  29. 29

    Dynamically add element to layout

HotTag

Archive