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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:32:08+00:00 2026-05-24T07:32:08+00:00

I have a javaScript function that updates a select inputs options. The function works

  • 0

I have a javaScript function that updates a select inputs options. The function works fine but I have to repeat the code for updating the field twice so that the update is done on page load as well as on parent select change(). This is the code that works:

jQuery.fn.filterOn = function(radio, values) {
  var firsttime = true;
  return this.each(function() {
    var select = this;
    var options = [];
    $(select).find('option').each(function() {
      options.push({
        value: $(this).val(), 
        text: $(this).text()
      });
    });
    $('#upload_range_manufacturer_category').data('options', options);
    $(radio).change(function() {
      var options = $(select).empty().data('options');
      var haystack = values[$(radio).val()];
      $.each(options, function(i) {
        var option = options[i];
        if($.inArray(option.value, haystack) !== -1) {
          $(select).append(
            $('<option>').text(option.text).val(option.value)
            );
        }
      });
    });
    if(firsttime){
      $(function() {
        var options = $(select).empty().data('options');
        var haystack = values[$(radio).val()];
        firsttime = false;
        $.each(options, function(i) {
          var option = options[i];
          if($.inArray(option.value, haystack) !== -1) {
            $(select).append(
              $('<option>').text(option.text).val(option.value)
              );
          }
        });
      })
    } 
  });
};

And the code for the select and function call:

<td><select name="upload_range[product_category]" id="upload_range_product_category"> 
<option value="2">Products</option> 
<option value="3">Manufacturers</option> 
</select></td> 
</tr> 
<tr> 
  <th><label for="upload_range_manufacturer_category">Manufacturer category</label></th> 
  <td><select name="upload_range[manufacturer_category]" id="upload_range_manufacturer_category"> 
<option value="4">Sports Equipment</option> 
<option value="6">Bricks</option> 
</select></td> 


<script type="text/javascript"> 
  $(document).ready(function () {
    var data = {"2":["4"],"3":["6"]};
    $('#upload_range_manufacturer_category').filterOn('#upload_range_product_category', data);
  }); 
</script>

To get rid of the duplication of code, I thought I could write a function that could be called from inside the current function. However my new code gives me this error:

Uncaught TypeError: Object function (a,b){return new e.fn.init(a,b,h)}
has no method ‘filterSelect’

This is the new code:

jQuery.fn.filterOn = function(field, values) {
  var firsttime = true;
  return this.each(function() {
    var select = this;
    var options = [];
    $(select).find('option').each(function() {
      options.push({
        value: $(this).val(), 
        text: $(this).text()
      });
    });
    $('#upload_range_manufacturer_category').data('options', options);
    $(field).change(function() {
      $.filterSelect(field,select,values);
    });
    if(firsttime){
      firsttime = false;
      $.filterSelect(field,select,values);
    } 
  });
};
jQuery.fn.filterSelect = function(field,select,values) {
  var options = $(select).empty().data('options');
  var haystack = values[$(field).val()];
  $.each(options, function(i) {
    var option = options[i];
    if($.inArray(option.value, haystack) !== -1) {
      $(select).append(
        $('<option>').text(option.text).val(option.value)
        );
    }
  });
};

If anyone has an idea what schoolboy error I have probably made, your input would be greatly appreciated. I have tried declaring the filterSelect function above and below the filterOn function but neither works.

Thanks

Luke

  • 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-24T07:32:08+00:00Added an answer on May 24, 2026 at 7:32 am

    Try removing .fn in your filterSelect declaration so it should look like

    jQuery.filterSelect = function(field,select,values) {
      var options = $(select).empty().data('options');
      var haystack = values[$(field).val()];
      $.each(options, function(i) {
        var option = options[i];
        if($.inArray(option.value, haystack) !== -1) {
          $(select).append(
            $('<option>').text(option.text).val(option.value)
            );
        }
      });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in my Javascript Code that updates html fields with their
I have a javascript function that manipulates the DOM when it is called (adds
I have a javascript function that calls a generic function to make an ajax
I have a javascript function that accepts a number and performs a mathematical operation
I have a javascript function that checks for a date range. Is there any
I have a javascript function that is being built to animate the collapse of
I have a javascript function that toggles the display for rows in a table.
I have a Javascript function that is called from the onchange method in a
I currently have a Javascript function that uses a string to reference an object
I have a javascript function (class) that takes a function reference as one paremter.

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.