3

I have a big form with at least 5 input date forms.

For example:

    <input type="text" id="nameClient" name="nameClient"/>
    <input type="text" id="nameRouterID" name="nameRouerID"/>
    <input type="text" id="dateInstallation" name="dateInstallation"/>
    <input type="text" id="dateConfiguration" name="dateConfiguration"/>
    <input type="text" id="dateConfirmationClient" name="dateConfirmationClient"/>

Well, i want to search the entire DOM for %date% word and apply datepicker jQuery plugin, avoiding to create this following lines for each element

    <script>
$( "#dateInstallation" ).datepicker();
$("#dateInstallation").datepicker("option", {dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true});
    });
    </script>';

I think it's possible to know with .find() function and "this" relative, but i'm not familiar. I think is something like this:

    $( "text" ).find( "%date%" ).datepicker();

And later:

    $(this).datepicker("option", {dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true});

But i can't get it work I am not sure whether it is syntax problem or other

2
  • 1
    Could you perhaps add a class to each of the elements? <input type="text" id="dateConfiguration" class="dateInput" name="dateConfiguration"/>, and then $('.dateInput').datepicker()? Commented Apr 8, 2014 at 9:45
  • Oh, I have not think that, it's a good idea if it's not possible to search in the entire DOM. I have to mark it, but the rest is dynamic. Commented Apr 8, 2014 at 9:48

1 Answer 1

3

You can use the attribute starts with selector to match ID's starting with date

 $('[id^="date"]').datepicker();

or to match any ID containing date, the attribute contains selector

 $('[id*="date"]').datepicker();

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

Edit: It works! To apply the specific option, I add after $('[id^="date"]').datepicker("option", {dateFormat: "dd/mm/yy", changeMonth: true, changeYear: true});

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.