Reputation: 1387
How can I add a dynamic created element with jquery in current element.
<div>
<script>
LoadWidget("Hello");
</script>
</div>
.
.
.
.
<script>
function LoadWidget(name){
//For example:
var widget = $("<p />").html(name);
document.write(widget.?????????);
}
</script>
Upvotes: 0
Views: 165
Reputation: 14649
Step 1
Add a <div>
with an ID of wiget
. This will be the container for the dynamically appended elements via your LoadWidget()
function.
<div>
<div id="widget"></div>
<script>
LoadWidget("Hello");
</script>
</div>
.
.
.
.
<script>
function LoadWidget(name)
{
if(name)
{
/*
* Use the widget HTML element to place the name
* into the widget container
*
*/
$('#wiget').html('<p>'+name+'</p>');
/* OR */
/* $('body').html('<p>'+name+'</p>'); */
}
else
{
return false;
}
}
</script>
Upvotes: 0
Reputation: 9850
If you actually want to get the html of an element, you can do
widget[0].outerHTML
or, more jQuery-esque:
widget.wrap("<div>").parent().html()
However, it is probably better for you to use the jQuery append
function to add your element to the page, by doing something like
$("body").append(widget)
Upvotes: 3