Is there a way for jQuery to change <div>
class based on when content was added? For example, If I have a page with multiple <div>
s and the page is updated with a new <div>
, can jQuery change the class for that <div>
?

- 188,624
- 52
- 326
- 405
-
Of course it can. If your question is "how can I do this", we will need to see your code to offer advice and guidance. – BenM Jan 26 '13 at 18:36
-
Yes you can use triggers – miru87 Jan 26 '13 at 18:36
-
This sort of depends on how the content is added – Explosion Pills Jan 26 '13 at 18:36
-
I figured, the code would be added manually. – Jan 26 '13 at 18:38
3 Answers
This answer seems to address your issues. Specifically the answer by Emile that suggests a generic JavaScript solution seems like a good starting point.
$("#content").html('something').triggerHandler('customAction');
$('#content').unbind().bind('customAction', function(event, data) {
//Custom-action
});

- 1
- 1

- 25,759
- 11
- 71
- 103
That depends on how your content is beeing updated. If you want to select new elements that appear after the user reloads the page (e.g. by pressing F5), a pure javascript/jquery solution won't help you. You'd need a server side solution that sends the creation dates of the content elements to the client or tells the client side which elements are new.
If you're trying to select elements that appeared due to a client-side action (without requiring the user to reload the page), Giacomo1968's answer might help you.

- 25,759
- 11
- 71
- 103

- 3,509
- 18
- 21
It would be easier for us to answer if you could share the code here. However, as I understand, you could use the combination of removeClass and addClass:
$("#div_id").removeClass("oldclass");
$("#div_id").addClass("newclass");
OR
$("#div_id").toggleClass("newclass");

- 114
- 4