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

  • SEARCH
  • Home
  • 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 9266155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:15:11+00:00 2026-06-18T14:15:11+00:00

I’ve been lurking a bit and couldn’t find the answer. Basically I have a

  • 0

I’ve been lurking a bit and couldn’t find the answer. Basically I have a bunch of buttons that I want to turn into a drop down menu and have the code be executed onChange. But, I’m new to javascript and I am having a hard time figuring out how this would work. I somewhat got it to work, but I couldn’t get it to work with more than one option. Here’s what I have:

<button class="lightbutton" onclick="lightswitch(1,true);lightswitch(2,true);lightswitch(3,true);">
All lights on</button> 
<button class="lightbutton" onclick="lightswitch(1,false);lightswitch(2,false);lightswitch(3,false);">
All lights off</button>

I got the lights to turn on by doing this:

<form name="functions">
<select name="jumpmenu" onChange="lightswitch(1,true);lightswitch(2,true);lightswitch(3,true);">
<option>LightFunctions</option>
<option value="*";>Light 1 On</option>
<option value="*";>Light 1 Off</option>
</select>
</form>

Now, I see why it works — it’s just telling it that whenever it changes to turn on all the lights. But how do I change the “onChange” to make it so it gets whichever option I have chosen?

I think I’m missing some JS but unsure.

I appreciate the help.

  • 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-18T14:15:13+00:00Added an answer on June 18, 2026 at 2:15 pm

    To have that select element control just the first lightswitch you can do this:

    <select name="jumpmenu" onChange="lightswitch(1,this.value==='on');">
    <option value="on";>Light 1 On</option>
    <option value="off";>Light 1 Off</option>
    </select>
    

    That is, instead of hardcoding true as the second parameter to lightswitch() test the current value of the select element. (Note that I’ve changed the value attributes to something more meaningful. The expression this.value==='on' will evaluate to either true or false.)

    Within the select’s onChange attribute this will refer to the select element itself.

    EDIT: To have the same select control multiple parameters you can add some data- attributes to the option elements to store as many extra parameters per option as needed (in this case I think you only need one extra). And I’d move the logic out of the inline attribute:

    <select name="jumpmenu" onChange="jumpChange(this);">
      <option value="">LightFunctions</option>
      <option data-switchNo="1" value="on";>Light 1 On</option>
      <option data-switchNo="1" value="off";>Light 1 Off</option>
      <option data-switchNo="2" value="on";>Light 2 On</option>
      <option data-switchNo="2" value="off";>Light 2 Off</option>
      <option data-switchNo="3" value="on";>Light 3 On</option>
      <option data-switchNo="3" value="off";>Light 3 Off</option>
    </select>
    
    function jumpChange(sel) {
       if (sel.value === "") return; // do nothing if user selected first option
    
       var whichLight = +sel.options[sel.selectedIndex].getAttribute("data-switchNo");
       lightswitch(whichLight, sel.value==='on');
    
       sel.value = ""; // reset select to display the "Light Functions" option
    }
    

    Demo: http://jsfiddle.net/N7b8j/2/

    Within the jumpChange(sel) function that I added the parameter sel will be the select element (set as this from the onChange attribute). The “magic” happens on this line:

    var whichLight = +sel.options[sel.selectedIndex].getAttribute("data-switchNo");   
    

    To explain that line: sel.options[sel.selectedIndex] gets a reference to the currently selected option, and .getAttribute("data-switchNo") gets that option’s data- attribute. The + converts the attribute from a string to a number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.