There is a variable:

var a = "someval"; 

Create a block with a class (variable a).

 $("#id").append('<div class="' + a + '"></div>'); 

Since a div with the class someval is created dynamically, the question arises: how then can such an action be performed?

 $(".someval").append("<p>Text...</p>"); 
  • Tell me. What does the title have to do with the question? - Pavel Mayorov
  • How then can you do this? - obviously you just need to call this code after adding - Grundy
  • Obviously ... But nothing works. What could be the reason? Maybe because the class name is created dynamically? - Vlad
  • Obviously ... But nothing works. - so you call wrong. The code above is quite working - Grundy
  • well, yes, the usual typo. Look at the line c div, with which you make the append first, not enough closing > - Grundy

1 answer 1

Everything works great.

 var a = "someval"; $("#id").append('<div class="' + a + '" > </div>'); $(".someval").append("<p>Text...</p>"); 
 .someval{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="id"> </div>