-1

Hy,

I have a small problem with jQuery

When I add a new input field, i can't use the click function on it :

var input = $('<input name="image" type="file"/>');
$(this).append(input);
input.click();

Any idea?

Thanks

gaymer
  • 442
  • 4
  • 9
  • 19
  • Are you trying to bind a handler on the click or are you trying to trigger a click event? – m90 Sep 20 '11 at 10:44
  • your code works, it executes the click function on an input type file element, here it opens a browse window to select a file. what do you want to achieve actually? binding something to be done when clicked? – Sander Sep 20 '11 at 10:45
  • possible duplicate of [In JavaScript can I make a "click" event fire programmatically for a file input element?](http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input-e) – Richard Dalton Sep 20 '11 at 10:45
  • This does work. Can you be more specific how it doesn't? – Sukima Sep 20 '11 at 10:45
  • actually this doen't work on chrome... – gaymer Sep 20 '11 at 10:56

1 Answers1

1

If you attach a click event handler to the new input, it will work fine:

var input = $('<input name="image" type="file"/>');
input.click(function() {
   //Do something
});
$(this).append(input);
input.click();

If on the other hand you want the new input to pick up an event handler that's been defined previously, you will have to use live when you attach the event initially (this example attaches a click event handler to all input elements, including ones that get added to the DOM later, like yours):

$("input").live("click", function() {
   console.log("other clicked"); 
});
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • it's seems that google chrome is my problem.... this is not working on chrome: ` ` but this yes... `` thanks google.... any idea to fix chrome problems? – gaymer Sep 20 '11 at 11:11