Skip to content

Common Custom JavaScript for GTM

Lauren Ancona edited this page Feb 13, 2017 · 1 revision

Useful snippets for custom HTML tags or JavaScript variables

Note: must be wrapped in a <script> tag if used in a custom HTML tag

Get text value from selected option of a dropdown menu

function() {
  var list = document.querySelector('select[name="sort"]');
  return list ? list.options[list.selectedIndex].value : undefined;  
}

where markup is:

<select name="sort" class="sortDropdown">
       <option value="BestMatch" selected="selected">Best Match</option>
       <option value="LowestPrice">Lowest Price</option>
      <option value="Rating">Rating</option>
</select>

source