Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8275095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:57:35+00:00 2026-06-08T07:57:35+00:00

I have a simple select box. When the user chooses an option, an image

  • 0

I have a simple select box. When the user chooses an option, an image and a text box containing some code should appear:

 <select name="state" id="state" style="margin-bottom: 1em;">
      <option value="AL">Alabama</option>
      <option value="AK">Alaska</option>
      <option value="AZ">Arizona</option>
      <!-- etc. etc. -->
 </select>
 
 <div id="badgeView"></div>
 
 <div id="codewrap" >
      <textarea id="codeView" readonly="readonly"></textarea>
 </div>

Here’s the script:

/* Helpful variables */ 
var codeTemplate = '<a href="http://www.nonprofitvote.org/voting-in-yourstate.html" title="Voting in Your State">\n' + '<img src="http://nonprofitvote.org/images/badges/voting_in_yourstate.jpg" alt="Voting in Your State" />\n' + '</a>';
var state = document.getElementById('state');
var badgeView = document.getElementById('badgeView');
var codeView = document.getElementById('codeView');

/* Load up AL's image and code when the page first loads */
changeBadgeAndCode();

/* What to do when the user selects an option */
state.addEventListener('change', changeBadgeAndCode, false);

/* What to do if a user clicks in the code view text box */
codeView.addEventListener('click', selectText, false);

/* Function declarations */
function changeBadgeAndCode(){
    changeBadgeView(badgeView);
    changeCodeView(codeView);

    var stateName = state.options[state.selectedIndex].value.toLowerCase();

    function changeBadgeView(element) {
        element.innerHTML = '<img src="http://www.nonprofitvote.org/images/badges/voting_in_' + stateName + '.jpg" />';
    }

    function changeCodeView(element) {
        codeTemplate = codeTemplate.replace(/yourstate/g, stateName);
        codeTemplate = codeTemplate.replace(/Your State/g, stateName);
        element.innerHTML = codeTemplate;
    }
}
function selectText(){this.select()}

If I alert stateName, I get a value that reflects my option choice. But that stateName variable doesn’t seem to make it to the changeBadgeView and changeCodeView functions. I can’t understand why. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:57:36+00:00Added an answer on June 8, 2026 at 7:57 am

    You’re invoking changeBadgeView and changeCodeView before the stateName has been assigned its value.

    You need to do the assignment first…

    function changeBadgeAndCode(){
        var stateName = state.options[state.selectedIndex].value.toLowerCase();
    
        changeBadgeView(badgeView);
        changeCodeView(codeView);
    
        function changeBadgeView(element) {
            element.innerHTML = '<img src="http://www.nonprofitvote.org/images/badges/voting_in_' + stateName + '.jpg" />';
        }
    
        function changeCodeView(element) {
            codeTemplate = codeTemplate.replace(/yourstate/g, stateName);
            codeTemplate = codeTemplate.replace(/Your State/g, stateName);
            element.innerHTML = codeTemplate;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have simple drop down box <select id=fruits> <option value=apple>Apple</option> <option value=orange>Orange</option> <option
I have the following PHP code doing a very simple select into a table.
I have a simple drop down box on a page with the below code:
I have a very simple Project form with a name field and a select
I have a simple form with one text box and a panel with three
I have a pretty simple form with a listbox, a text box, and two
I have developed a very simple Android app where user has to select items
I built a simple block module with a select box (where a user can
I have one simple User Registration form. It includes name, age, sex & city.
I have an asp.net web page with a simple search text box that returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.