JavaScript Variable and Form Interaction

I have a form structured like this with roughly 20 options:

<select name="appIdId">
      <option value="56T34AKJJR">Treasure Wheel Slots</option>

I have the option text in a variable like this:

apptext = "Treasure Wheel Slots";

What I need to do is get the option value (56T34AKJJR) and store that in a variable I can use later on in my code.

apptextvalue = "56T34AKJJR";

Keep in mind I will not be selecting any options, just passing the apptext into the form and returning apptextvalue.

I am not a JavaScript expert, and I would like some guidance on how to pull this off. Is this feasible?

Thank you.

Shawn JanesAsked:
Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
Using jQuery :
var apptext = "Treasure Wheel Slots";

$(document).ready(function() {
    var apptextvalue =  $("select[name=appIdId] option").filter(function() { return $(this).text()==apptext; }).first().val();

    // checking

Open in new window

RobOwner (Aidellio)Commented:
So you're wanting to match the string stored in the variable with an item in the list?
That's possible and involves just looping through each value till you find what you're after. I'll post an example when back at the laptop
Test page :

var apptext = "Treasure Wheel Slots";

window.onload = function() {
    var apptextvalue = getValueFromText(apptext);

    // checking

var getValueFromText = function(txt) {
    var dropdown = document.getElementsByName("appIdId")[0]; // add an ID and use getElementById instead getElementsByName for $0 !!!
    for(var i=0;i<dropdown.options.length;i++) if(dropdown.options[i].text == txt) break;
    return (i != dropdown.options.length)?dropdown.options[i].value:null;

Open in new window

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

RobOwner (Aidellio)Commented:
beat me to it :) thanks, like your example @leakim971
Michel PlungjanIT ExpertCommented:
That is a solution to the perceived problem as presented by shawnjanes.
I would like to know WHY you have apptext in a variable and not the accompanying value.

What is the server process that generates the select and the text?

You may simply miss some logic that sets up the needed values on the server.

Do you have more of these? If so you could possibly simply do a json_encode of the object that generates the select, it could be created in such a way that you just needed to do
echo "appTextValue ='".$someArray[$someText]."'";
somewhere (above examples are PHP)
Shawn JanesAuthor Commented:
Thank you. This worked perfectly. I can finally move on to the next problem.

::minor celebration going on here::
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.