I want to add text to an existing div, when I click the Button with the id #add. But this is not working.
Here my code:
$(function () {
$('#Add').click(function () {
$('<p>Text</p>').appendTo('#Content');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="Content">
<button id="Add" />
</div>
I hope you can help me.
You need to define the button text and have valid HTML for the button. I would also suggest using
.on
for the click handler of the buttonAlso I would make sure the jquery is at the bottom of the page just before the closing
</body>
tag. Doing so will make it so you do not have to have the whole thing wrapped in$(function
but I would still do that. Having your javascript load at the end of the page makes it so the rest of the page loads incase there is a slow down in your javascript somewhere.Html
if you want after the buttom
or before
Very easy from My side:-
Your html is invalid
button
is not a null tag. Trywe can do it in more easy way like by adding a function on button and on click we call that function for append.