/    Sign up×
Community /Pin to ProfileBookmark

Return Selected Drop Down Item in a form

Ok I have been trying all sorts of things trying to figure this out and am failing miserably. I am using Google Tag Manager and following along on one of Simo’s blog posting. In the section where he details how to capture the selected item he uses code:

[CODE]function() {
var selectList = {{Form Element}}.querySelector(‘#selectListId’);
return selectList ? selectList.options[selectList.selectedIndex].value : undefined;
}[/CODE]

Context: “To capture the selected item value in a drop-down list, checking for the value of the list itself will not work, as you’d intuitively expect. Instead, you need to access the option in the list that was selected, and then capture its value. Description: First, the script retrieves the drop-down list with ID selectListId. Next, it returns the value of the selected option in the list. If the list does not exist, the script returns undefined.

The form I am working with uses classes instead of ID’s for the elements so I would have thought using “.” instead of “#” would solve my problem but no luck. Ie I used:

[CODE]function() {
var selectList = {{Form Element}}.querySelector(‘.SelectDropdown.js-dropdown-usertype’);
return selectList ? selectList.options[selectList.selectedIndex].value : undefined;
}[/CODE]

Any solutions on how to properly make this work as a custom javascript variable?

Here is the html I am working with:

[CODE]<form id=”submit_interest_fish” class=”js_prevalidation_form”>

<select class=”SelectDropdown js-dropdown-usertype” required=””>

<option value=”New” selected=””>I’m New Here</option>

<option value=”Pro”>I’m a Pro User</option>

</select>[/CODE]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Matty2Shoes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...