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>
</select>

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.
Shawn JanesProject Manager | Sr. DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
0
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/M6taW/

var apptext = "Treasure Wheel Slots";

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

    // checking
    alert(apptextvalue);
}

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

0
leakim971PluritechnicianCommented:
Using jQuery : http://jsfiddle.net/M6taW/1/
var apptext = "Treasure Wheel Slots";

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

    // checking
    alert(apptextvalue);
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

RobOwner (Aidellio)Commented:
beat me to it :) thanks, like your example @leakim971
0
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)
0
Shawn JanesProject Manager | Sr. DeveloperAuthor Commented:
Thank you. This worked perfectly. I can finally move on to the next problem.

::minor celebration going on here::
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.