-1

I was wondering if was there some way to do a rule for all id who end by "At", so that would choose automatically all my datetime input id going from there monthly_startAt to medicines_startAt so that would make our websites more autonomous about the pick and then be reducing the code wrote from us.

<script>
    $("#monthly_startAt").datepicker({
        inline: true
    })

    $("#daily_endAt").datepicker({
        inline: true
    })

    $("#monthlyo_paidAt").datepicker({
        inline: true
    })
</script>

To just one line like for example

<script>
    $(allHowEndWith -> "#At").datepicker({
        inline: true
    })
</script>
  • The linked question will answer the question you've asked, but a better solution would be to add a class to all the elements and simply use `$(".your-class-name")` to select them. – Reinstate Monica Cellio Aug 21 '18 at 10:21
  • @Archer before I tried to learn the post, but I actually don't understand if that could solve this problem. The answer of Ankit Agarwal solve my problem much better, because of my low level of javascript knowledge is low right now and the way he responded is much simpler to understand. So I should delete the post up in the same? – jeremytrindade Aug 21 '18 at 10:49
  • It's the exact same answer. You could remove the upvote if you like, but there's no reason as the answer here is correct. Ideally you should upvote the answer below *and* the linked answer. – Reinstate Monica Cellio Aug 21 '18 at 10:51
  • @Archer I can't vote up anyone because I have no points for that the minimum for upvote is 15 and I only have 8 yet. – jeremytrindade Aug 21 '18 at 15:22
  • Don't worry about it - it's really not anything :) – Reinstate Monica Cellio Aug 21 '18 at 15:26
  • @Archer do you know why when I use it on Symfony I receive this error message: new:56 Uncaught ReferenceError: $ is not defined? – jeremytrindade Aug 21 '18 at 15:32
  • 56 contains: $("[id$='At']").datepicker({ – jeremytrindade Aug 21 '18 at 15:33
  • Most likely you've not included jQuery before that line of code is executed. Check your script includes are in the right order. – Reinstate Monica Cellio Aug 21 '18 at 15:38
  • @Archer I did, but maybe is the way how Symfony4 and twig works make it doesn't happen because they have their own way. I found it easier i just need to put this on the form file if i put this: ->add('startAt', DateType::class, array( 'required' => true, 'widget' => 'single_text', 'attr' => [ 'class' => 'form-control input-inline datepicker', 'data-provide' => 'datepicker', 'html5' => false, ], )) – jeremytrindade Aug 21 '18 at 17:16
  • @ElnurAbdurrakhimov sorry for calling you there be I found your answer(https://stackoverflow.com/questions/8713200/set-default-value-on-datetime-field-in-symfony2-form) and I can't comment, because I don't have 50 points yet. How can I actually call the date as a placeholder after saving it on Entity? – jeremytrindade Aug 21 '18 at 22:32

1 Answers1

2

You can use the selector "[attribute$='value']" as it will select elements that have the specified attribute, id in your case, with a value ending exactly with a given string. Also note that the comparison is case sensitive.

$("[id$='At']").datepicker({
  ...
});

Below is the simple example to illustrate the working of end at selector:

$("[id$='At']").css('color', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='monthly_startAt'>monthly_startAt</div>
<div id='monthly_endAt'>monthly_endAt</div>
<div id='monthly_paidAt'>monthly_paidAt</div>
<div id='random1'>random 1</div>
<div id='random2'>random 2</div>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • That solves my problem. – jeremytrindade Aug 21 '18 at 10:24
  • @jeremytrindade glad to help – Ankit Agarwal Aug 21 '18 at 10:39
  • btw at the end it doesn't work for what I need xD I don't know why but it doesn't work on Symfony4 but no problem because I found an easier way to do it directly on form file: ->add('startAt', DateType::class, array( 'required' => true, 'widget' => 'single_text', 'attr' => [ 'class' => 'form-control input-inline datepicker', 'data-provide' => 'datepicker', 'html5' => false, ], )) – jeremytrindade Aug 21 '18 at 17:19