1

How do I bind a function to future elements (elements that are added at run time)? for example, see the code below:

$('input, textarea').placeholder();

I want to bind the placeholder function all the yet to be in the document "input" and "textarea"

P.S. I know you can use jquery's "on" method to bind to the events but not very sure how i bind functions (placeholder in this case) to future elements.

thanikkal
  • 3,326
  • 3
  • 27
  • 45
  • 1
    There are other similar questions:http://stackoverflow.com/questions/6781661/jquery-how-to-call-a-jquery-plugin-function-on-an-element-that-hasnt-yet-been – Claudio Redi Jun 06 '13 at 01:47
  • Thanks @ClaudioRedi. that link has some work around to address this. – thanikkal Jun 06 '13 at 01:52

2 Answers2

1

You can only bind elements to events, for the future elements you need to use the delegation:

$(document).on('click', 'input, textarea', placeholder);

See http://api.jquery.com/on/ for more information on what event you can bind, and how to do it.

Antoine
  • 2,785
  • 1
  • 16
  • 23
0

You can't. You can only do this with events because of event propagation delegation. You will need to set up the placeholder on any new elements as they are inserted.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251