tags:

views:

45

answers:

4

I'm writing an input button with the following code:

$("#QnA").append("<input type='button' name='questionSub' value='Save'/>");

What I would like to do it capture the click function of that new button. This is what I attempted:

$('input[name=questionSub]').click(function () {
   alert('hi');
});

This isn't working for me. Any ideas?

+1  A: 

It works for me. (jsFiddle Example)

Just make sure you call the click function after you've appended the input button.


Edit:
You only need to use live() if you want to bind the click function before you append the input, since live() will attach a handler to the event for all elements which match the current selector, now or in the future.

jsFiddle Example

Peter Ajtai
That is my code too but it wouldn't work! However the .live (@Marko) solution did work. Thanks.
Jeff V
@Jeff V - You should only need live if you append the INPUT after trying to bind the click.
Peter Ajtai
@Peter - Can you expound on this further? The click function is "after" the append code. Not sure if that is what you are saying. The .live is new to me. It is very possible that I'm doing something incorrect in my setup code. For whatever reason the code in your fiddler example (which is the same as my original code) doesn't work.
Jeff V
@Jeff - Post the entire pertinent region or a link to the entire pertinent region (try reproducing it on jsFiddle or just the url of the orginial), since it does sound like you do not need to use live, so you are doing some extra operation you aren't aware of.
Peter Ajtai
I am making an ajax call to my webservice (asp.net) and then looping through the JSON data. At the end of my loop I am doing the append statement for the button. All of this code is from with in the success: of the $.ajax - not sure if that has anything to do with my issue. I appreciate you trying to help.
Jeff V
@Jeff - Interesting. It still seems like you should not need live(). The only other thing I can think of is that the element with ID `QnA` must also already be a part of the document before you append the input button to it. Are you doing any appending outside the success: ? Try making a simple test page using your server and play around with that....
Peter Ajtai
+1  A: 

You need to use jQuery's live method for newly created elements

$('input[name=questionSub]').live("click", function () {
   alert('hi');
});
Marko
This worked! Thanks.
Jeff V
+3  A: 

You can only bind an event to an element after the element has been created so make sure that the click binding occurs after the input has been appended.

Justin Lucas
number 1 is not true, and number 2.... is, umm... not true :)
Marko
@Marko - #2 *is* true, you can only bind an event *to the element* once it's there. `.live()` doesn't bind *to the element*, it binds to `document` :)
Nick Craver
Okay I take my downvote back. Damn you Craver :)
Marko
Wasn't 100% sure about number 1 hence "I believe...".100% sure that number 2 is true. You can't bind an event to an element that does not yet exist.
Justin Lucas
I had to edit your question so I can re-vote. I've given you +1 cuz I'm oh so very wrong :)
Marko
I probably only deserve half a point but thanks for the re-vote.
Justin Lucas
@Justing - Please edit your answer and delete #1 as it is incorrect. All you have to do is look at http://api.jquery.com/attribute-equals-selector/ , ` Quotes are optional` for the attribute value.
Peter Ajtai
+3  A: 

You can attach the click handler when you create it, like this:

$("<input type='button' name='questionSub' value='Save'/>").click(function() {
  alert('hi');
}).appendTo("#QnA");
Nick Craver
I might be able to do that but the code might be much more involved than just the alert.
Jeff V
@Jeff V quite possibly true. If you like to preserve the visibility you can put your code within a function and call that function - it is a design choice, but quite possible this is a better solution than .live because it binds to the #QnA id and thus should be more performant than bound to the document.
Mark Schultheiss
Jeff V