-2

So, I want to create an onclick event with javascript for each option of a "select form" when clicked, but since drupal doesn't use HTML I can't modify it manually and put an id to each. This is the exact element I want to change (pic below). Can I do it modifying the php of the theme? I took a look at it but didn't make sense to me. (This is the Chrome inspect tool)enter image description here

  • You can try using the onchange event with #ajax property on that form field. – Kevin Dec 3 '18 at 13:01
1

Actually <option> is not a valid target, see this: https://stackoverflow.com/a/1402268/9976763

You should bind the https://api.jquery.com/change/ event for the <select> instead and check if the given option is selected or not. For that, you do not need to put an ID on the option.

Also check https://www.drupal.org/docs/7/api/javascript-api on how to write Javascript properly for Drupal.

  • Thanks, man. That's exactly what I was looking for. – Bruno Capriglioni Dec 3 '18 at 13:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.