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 592749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:46:21+00:00 2026-05-13T15:46:21+00:00

I have the following bit of code. I’m trying to dynamically add drop-down options

  • 0

I have the following bit of code. I’m trying to dynamically add drop-down options to a second drop-down based on the selection in a first drop-down. Both are generated through an Ajax call to a script that pulls the data from a database. Here is the relevant code.

 function setSub(doc) {
  // Setup some variables
  var sNode = document.getElementById('4'); // GRAB THE ELEMENT HERE
  var rmiData = doc.getElementById('rmiData').innerHTML;
  var aItems = rmiData.split("|");
  var aVals;
  // Set some background
  sNode.style.background = "#fff";
  // Loop through adding options to the drop down
  for (var x = 0; x < (aItems.length -1); x ++) {
   aVals = aItems[x].split("=");
   // ADD OPTIONS DYNAMICALLY TO SECOND DROP-DOWN HERE
   sNode.options[x+1] = new Option(aVals[1],aVals[0],false,false);
   // PROBLEM HERE: ADD ONCLICK TO THE OPTION WE JUST CREATED
   sNode.options[x+1].onclick = function() { fixDayList(); };
   //sNode.options[x+1].onclick = new Function("fixDayList();");
   //sNode.options[x+1].setAttribute("onclick", "fixDayList();");
   // END PROBLEM LINES
  }
  // Set the length
  sNode.length = x+1;
  // Select the first item in the list
  sNode[0].selected = true;
 }

If you notice the section after the “// PROBLEM HERE” line, I’ve tried to add the onclick event in three different ways. All 3 work correctly in Firefox. None of the 3 work in IE6 or Safari (Not sure on IE8). Any suggestions for this? The rest of the code is using some old-schoolish JavaScript that I copied from an existing piece of legacy code that works and I’m just trying to add onclick to my version (preferably without re-writing a lot of code).

Any thoughts on why the onclick events don’t fire from Safari and IE?

I do have access to jQuery in my code. So, if jQuery fixes this internally and I can figure out the proper selector to use, that would work. But, I’m not sure how to select this, exactly. I tried:

$(‘#4’).options[x+1].click( function() { fixDayList(); } );

But that selector is bad and I’m not sure exactly how to select the right one (I’m green in the ways of jQuery).

  • 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-05-13T15:46:21+00:00Added an answer on May 13, 2026 at 3:46 pm

    Use something like this:

    if(window.addEventListener){
       sNode.options[x+1].addEventListener('click', fixDayList, false);
    } else if (window.attachEvent){
       sNode.options[x+1].attachEvent('onclick', fixDayList);
    } else {
       sNode.options[x+1].onclick = fixDayList;
    }
    

    Update:

    In Safari, unless the option elements are visible and not displayed as a dropdown list, they will never trigger a click event. But, if you add size=10 or whatever to your select element, Safari will then trigger click events on the option elements.

    Your best bet will be to listen for the change event on the select element instead of trying to use click on the options.

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

Sidebar

Related Questions

I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I have the following bit of code which I'm just trying out by running
I have the following bit of code in a method called by clicking the
I have the following bit of code: protected function onEnterFrame(e:Event):void { var diff:Number; //
I have the following bit of code that worked as expected before we upgraded
I have the following bit of code in a class method NSDictionary *shopAddresses =
I have the following bit of code, I expect that given cstdio is included
I have the following bit of code that is timing out when I pass
I have the following bit of code which is responsible for displaying a tooltip.
I have the following bit of legacy C++ code that does not compile: #include

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.