Avatar of Shawn Janes
Shawn JanesFlag for United States of America asked on

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.

JavaScriptHTMLScripting Languages

Avatar of undefined
Last Comment
Shawn Janes

8/22/2022 - Mon

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 : http://jsfiddle.net/M6taW/

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


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

beat me to it :) thanks, like your example @leakim971
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Michel Plungjan

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 Janes

Thank you. This worked perfectly. I can finally move on to the next problem.

::minor celebration going on here::