1

How to output a div inside another div with jQuery

first principal div :

<div id ="username_error"></div>

and this one will be inside it :

 <div class="alert alert-primary" role="alert">
    This is a primary alert—check it out!
 </div>

I tried

$("#username_error").html('<div class="alert alert-primary" role="alert">
  This is a primary alert—check it out!
    </div>
');

but it doesn't work , is there an alternative method thanks in advance

Shiladitya
  • 12,003
  • 15
  • 25
  • 38

4 Answers4

0
var content = '<div class="alert alert-primary" role="alert">This is a primary alert—check it out</div>';

$("#username_error").html(content);
Zahid Saeed
  • 152
  • 2
  • 9
0

Just add this to your JQ:

$("#username_error").html('<div class="alert alert-primary" role="alert">This is a primary alert—check it out!</div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="username_error">

</div>
Zvezdas1989
  • 1,445
  • 2
  • 16
  • 34
0

you can use append function

$("#username_error").append('<div class="alert alert-primary" role="alert">This is a primary alert—check it out!</div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="username_error">

</div>
samehanwar
  • 3,280
  • 2
  • 23
  • 26
0

$('<div class="alert alert-primary" role="alert">This is a primary alert—check it out!</div>').appendTo("#username_error");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id ="username_error">

</div>
samehanwar
  • 3,280
  • 2
  • 23
  • 26