1

I want to add Jquery date picker to input elements created from for loop in PHP.

Say i use 'for' loop and generate 5 input elements:

 for($i=0;$i<=5;$i++)
 {
  echo '<input class="datepicker_input"/>';
 }

Now if i try attaching JQuery date picker

 $(window).load(function(){
    $(".datepicker_input").datepicker();
 });

This makes datepicker option to displayed to all elements generated, but when I select the second input and change date, the first input gets changed and the event is not binded to other input elements.

How can we make date picker option to be applicable to all input elements..

3
  • 4
    Other than the missing . in the class selector it is fine jsfiddle.net/arunpjohny/Ht74L/1 Commented Aug 6, 2014 at 5:23
  • @arun please post your answer as solution Commented Aug 6, 2014 at 5:31
  • Hi Arun, In fiddle it works fine as we create elements manually. But when we loop and create the elements it doesnot work. Please try to create elements in php using loop & check.. Commented Aug 6, 2014 at 6:27

2 Answers 2

1

firstly your selector

$("datepicker_input") is incorrect,
//it should be $(".datepicker_input").datepicker();

and then use and if you want to bind multiple items then go for jquery each. eg-

$('selector').each(function(){
$(this).datepicker();
});
Sign up to request clarification or add additional context in comments.

1 Comment

HI, accepted my mistake, it was a typo error. But still each function does not work in this case...
0

Thanks for the help extended... I got the solution. Only i needed to have id for the input element.

Solution:

To create elements dynamically:

  for($i=0;$i<=5;$i++)
  {
   echo '<input id="elementid'.$i.'" class="datepicker_input"/>';
  }

In JQuery:

 $(function() {
    $('.qwer').each(function(){
        $(this).datepicker();
    });
 });

It works now...

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.