/    Sign up×
Community /Pin to ProfileBookmark

Selecting values from a dropdown menu

This might be an incredibly easy problem to fix, but I’m not quite sure what to do. Been looking around the net and have come up pretty empty, so I was hoping someone would be kind enough to help me out. I’ve never used JS/JQuery much, but I have a small task I need to get done.

There is a dropdown menu with a list of twenty or so options . I need to write a function that selects a specific list of them and adds them to another list I have.

So far I have this (keep in mind I didn’t write most of this; the guy who did is out and I have to get this done, or else I’d just wait and ask him):

[CODE]
$(“#button”).click(function(event){

var itemValue = $(‘#dropdownMenu’).val()

var defaultValues = [1001,1012,1009,1011,1014,1026,1030,1041,1017]
// this list of values is a partial list of everything in dropdownMenu
// I want to add these to otherList, below

var otherList = $(“#otherList”).val().split(“|”)

$.each(defaultValues, function(itemValue) {
if(jQuery.inArray(itemValue, otherList) < 0) {
itemText = $(“#dropdownMenu :selected”).text();

idx = $(“#dropdownMenu”).attr(“selectedIndex”);
$(‘#someTable tr:last’).after(‘<tr id=”ar_’ + idx + ‘”><td>’ + itemText + ‘</td></tr>’);

$(‘#otherList’).val($(‘#otherList’).val() + ‘|’ + itemValue);

if ($(‘#otherList’).val().charAt(0) == “|”) {
$(‘#otherList’).val($(‘#otherList’).val().replace(‘|’, ”));
}
}
});
});
[/CODE]

Now, obviously this just adds the dropdownMenu value eight times to the list because it’s just using the value of whatever is selected in the menu, rather than the defaultValues list that I want (the original function was just adding the selected item; I am adding the #button and new code to add the defaultValues list to otherList). Just not quite sure how to get the value and text from my defaultValues array in order to display them correctly in that otherList array.

If this isn’t clear, I can try to clarify if needed. Thanks for any help. ?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @hammerpants 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...