views:

34

answers:

3

I want to create some DIV from javascript. I don't know how many I will need to create, but I will need to change the back-color of it after a while.

I'm using jQuery in my project. So if it's easier the solution can use jQuery.

+1  A: 

Give them a class name that you can later refer to.

Iker Jimenez
+1  A: 

Give them a class name, and then refer to it in jQuery like so:

<div class="something">Hello</div>
<div class="something">Hi</div>

<script type="text/javascript">
$(document).ready(function(){
  $('div.something').css('background-color', 'white'); // Or whatever
});
</script>
James Skidmore
+2  A: 
var $myDiv = $('<div />');

This will create a div inside the variable $myDiv.

You can change any property of it later on, and you can even add it to your page, like:

$myDiv.css('background-color', '#FFF').appendTo('#anotherDiv');
Alex Bagnolini