0

Using webform, I have two "Date/time" elements with popup date and timepickers enabled. I have a jQuery script to calculate and display detailed time difference between the values (date and time) entered in the two elements fields and for that I use those elements' id's several times.

My question is: I like to have custom id's one for each date and time element's field. Drupal/Webform automatically assign long ids like e.g. #edit-leaving-time-time for the time filed and #edit-leaving-time-date for the date field. That's for the first Date/Time element. And likewise, there's #edit-returning-time-time for the time filed and #edit-returning-time-date for the second Date/Time element.

The option in "Element custom attributes (YAML)" (accessed from the webform element Advanced tab on editing) to add a new id affects the whole Date/Time element's both date and time while I need to have unique id for each (one for the date and another for the time) to use in my jQuery script.

I see on twig debugging that those elements are controlled by the file \web\core\themes\classy\templates\form\input.html.twig but when I opened it, I only found this line and so I couldn't make any sense to where to go from there.

<input{{ attributes }} />{{ children }}

Although everything is working fine now and my question is more about "cosmetic" thing than functionality, I still love to learn if there's any way to do it without having to write a custom module or so since I still have no experience in that.

Thanks so much for any insightful ideas you may have.

  • Question indeed is: Why? You can target them easily with just the parent's IDs and their classes. You could start with implementing hook_preprocess_input from your theme, but I doubt you'll have a marker to identify them uniquely. So you'd first need to add them a unique enough attribute from hook_form_alter first, probably. – leymannx Jun 16 at 6:34
  • @leymannx thanks for your help but it seems I still need a lot of learning and, unless I can have a practical example or step by step how-to info to mimic I'll leave this for now till I grab enough concept of what you mentioned and now focus on the more important ones like my other webform question drupal.stackexchange.com/questions/282349/… – Adam888 Jun 16 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.