1

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?

0

4 Answers 4

3

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

$('input[name=questionSub]').live("click", function () {
   alert('hi');
});
Sign up to request clarification or add additional context in comments.

Comments

3

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");

3 Comments

I might be able to do that but the code might be much more involved than just the alert.
@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 - I might use this solution in a different part of the app. Thanks!
3

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.

5 Comments

@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 :)
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.
I had to edit your question so I can re-vote. I've given you +1 cuz I'm oh so very wrong :)
I probably only deserve half a point but thanks for the re-vote.
@Justing - Please edit your answer and delete #1 as it is incorrect. All you have to do is look at api.jquery.com/attribute-equals-selector , ` Quotes are optional` for the attribute value.
1

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

6 Comments

That is my code too but it wouldn't work! However the .live (@Marko) solution did work. Thanks.
@Jeff V - You should only need live if you append the INPUT after trying to bind the click.
@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 - 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.
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.
|

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.