1

I want the user to click on a button and then have some html pop into an element. I did so and it works but it only works once. If they click the button again, nothing happens. I thought using empty() would fix the problem but it doesn't. What's wrong with my code.

<script type="text/javascript" >

$(document).ready( function() {

   $('#button').on('click', function () {

     $('#ul').html('<li>testing testing</li>').hide(1000, function() {

        $(this).empty();    

     });    

   });

});

</script>

<input type="button" value="click me" id="button" />

<ul id="ul">

</ul>

5 Answers 5

3

The element is hidden the second time you click the button. You can do:

$('#ul').show().html('...

http://jsfiddle.net/KXkgZ/

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way I could target the newly inserted li in the ul and just hide that? Actually I think I can do that on my own.
I don’t understand your comment, sorry :) If you have another question, it’s probably better to post a new topic.
@jason328 using your current method, you are replacing the ul content with your li each time so there is only one at any given time
@Huangism yes, that is how I interpreted "pop some html into an element".
@David sorry my comment was meant for the OP's comment
2

as you want to pop html each time you click on button you should use append

$('#ul').append('<li>testing testing</li>');

see append

Comments

2
$("#button").click(function(){ 
    $("#ul").append("testing testing");
});

Comments

1
jQuery('<li>testing testing</li>').appendTo('#ul').hide(1000, function() {
    $(this).remove();    
 });    

Comments

1

When you do a .hide() you set the CSS selector "display" = "NONE" You need to change it to something like this:

$('#ul').html('<li>testing testing</li>').hide(1000, function() {
  $(this).empty();
  $(this).show();
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.