Skip to content

Instantly share code, notes, and snippets.

@froboy
Created August 20, 2024 14:32
Show Gist options
  • Save froboy/2de2cd8d88ac32c03d34bb41513453b2 to your computer and use it in GitHub Desktop.
Save froboy/2de2cd8d88ac32c03d34bb41513453b2 to your computer and use it in GitHub Desktop.
Grab options from a HTML select list and output them in YAML
// Grab all of the options from a select list and dump them to the console in YAML format
// so that it can be consumed by Drupal webform source (or anything else that accepts YAML).
// 'Label': 'value'
// Replace the selector and then paste into your console.
// jQuery method
let opts = '';
jQuery('#00Z1z00000XX1XX option').each(function() {opts += ("'" + this.label + "': '" + this.value + "'\n")});
console.log(opts)
// Pure JS method
// If your selector is something that begins with a digit like a Salesforce ID (e.g. "00N3h00000IXX0X")
// it's not technically a valid CSS identifier. You can either escape it, or just use a different selector.
let opts = '';
document.querySelectorAll("[id='00Z1z00000XX1XX'] option").forEach((option) => {opts += ("'" + option.label + "': '" + option.value + "'\n")});
console.log(opts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment