How to reuse function in different files?

worldwildwebdev

I'm using this function already in 5 different files for different ID fields. And when I fill it with more data, will be a lot of same code in my pages. Is there any way just to call that specific items array from another file? I'm new to JavaScript and jQuery.

var items = [{
  name: '-Моля,изберете-',
  value: '',
  subitems: [{
    name: '-Моля,изберете-',
    value: ''
  }]
}, {
  name: 'BMW',
  value: 'bmw',
  subitems: [{
    name: '-Моля,изберете-',
    value: ''
  }, {
    name: '320',
    value: '320'
  }, {
    name: '330',
    value: '330'
  }]
}, {
  name: 'Toyota',
  value: 'toyota',
  subitems: [{
    name: '-Моля,изберете-',
    value: ''
  }, {
    name: 'MR2',
    value: 'mr2'
  }, {
    name: 'Capry',
    value: 'capry'
  }]
}];


jQuery(document).ready(function($) {

  var temp = {};
  $.each(items, function() {
    $("<option />")
      .attr("value", this.value)
      .html(this.name)
      .appendTo("#adm_car_make");

    temp[this.value] = this.subitems;
  });

  $("#adm_car_make").change(function() {
    var value = $(this).val(),
      menu = $("#adm_car_model");

    menu.empty();

    $.each(temp[value], function() {
      $("<option />")
        .attr("value", this.value)
        .html(this.name)
        .appendTo(menu);
    });

  }).change();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="adm_car_make"></select>
<select id="adm_car_model">
  <select>

Rob Schmuecker

You can actually split almost all functionality out to a separate file which takes 2 arguments (makeField and modelField) as a function to markup your pages.

// Start of first file declare the function and the data once.
var myCars = [{
    name: '-Моля,изберете-',
    value: '',
    subitems: [{
        name: '-Моля,изберете-',
        value: ''
    }]
}, {
    name: 'BMW',
    value: 'bmw',
    subitems: [{
        name: '-Моля,изберете-',
        value: ''
    }, {
        name: '320',
        value: '320'
    }, {
        name: '330',
        value: '330'
    }]
}, {
    name: 'Toyota',
    value: 'toyota',
    subitems: [{
        name: 'MR2',
        value: 'mr2'
    }, {
        name: 'Capry',
        value: 'capry'
    }]
}];


jQuery(document).ready(function ($) {
    // Make a reusable function
    window.makeCarsDropdown = function (makeField, modelField) {
        var temp = {};
        $.each(myCars, function () {
            $("<option />")
                .attr("value", this.value)
                .html(this.name)
                .appendTo(makeField);

            temp[this.value] = this.subitems;
        });

        $(makeField).change(function () {
            var value = $(this).val(),
                menu = $(modelField);

            menu.empty();

            $.each(temp[value], function () {
                $("<option />")
                    .attr("value", this.value)
                    .html(this.name)
                    .appendTo(menu);
            });

        }).change();
    }
});
/* End of first file */

Then you can call the makeCarsDropdown function anywhere after you have included the above file, passing it two selectors

// Call the function from anywhere e.g.
jQuery(document).ready(function ($) {
    makeCarsDropdown("#adm_car_make", "#adm_car_model");
});

Working snippet:

// Start of first file declare the function and the data once.
var myCars = [{
  name: '-Моля,изберете-',
  value: '',
  subitems: [{
    name: '-Моля,изберете-',
    value: ''
  }]
}, {
  name: 'BMW',
  value: 'bmw',
  subitems: [{
    name: '-Моля,изберете-',
    value: ''
  }, {
    name: '320',
    value: '320'
  }, {
    name: '330',
    value: '330'
  }]
}, {
  name: 'Toyota',
  value: 'toyota',
  subitems: [{
    name: '-Моля,изберете-',
    value: ''
  }, {
    name: 'MR2',
    value: 'mr2'
  }, {
    name: 'Capry',
    value: 'capry'
  }]
}];


jQuery(document).ready(function($) {
  // Make a reusable function
  window.makeCarsDropdown = function(makeField, modelField) {
    var temp = {};
    $.each(myCars, function() {
      $("<option />")
        .attr("value", this.value)
        .html(this.name)
        .appendTo(makeField);

      temp[this.value] = this.subitems;
    });

    $(makeField).change(function() {
      var value = $(this).val(),
        menu = $(modelField);

      menu.empty();

      $.each(temp[value], function() {
        $("<option />")
          .attr("value", this.value)
          .html(this.name)
          .appendTo(menu);
      });

    }).change();
  }
});
/* End of first file */

jQuery(document).ready(function($) {
  makeCarsDropdown("#adm_car_make", "#adm_car_model");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="adm_car_make"></select>
<select id="adm_car_model"></select>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jmeter: How to reuse custom java function between different JMX file

How to reuse a method from a different class

how to reuse a function in multiple controllers

How to reuse the same tests to test different implementations?

How to reuse code in an R function?

How to: Docker reuse layers with different base images

How to reuse the same component instance on different urls

Reuse a 'mixin' with Styled Components across different files?

How to reuse a JS function on different HTML elements

How to reuse an output of a function?

How to reuse .csx files

How to reuse operator overloads for different types?

Is there are way to reuse a function with different values in a code?

How to make a loop to reuse a function

how can I reuse the function to update different parts of the state of a React component?

How to name and reuse a javascript function

How to reuse a code which is slightly different

Reuse function in two different controllers in angularJS

How do I Reuse an InjectionToken with a different provider

How to reuse the call function in r?

How to write a function that runs that function on 20 different csv files in Python?

How can I reuse a function for many different ID values?

How to reuse argument from a function?

How to reuse deps for different tests in Bazel?

reuse function with different variables

How to reuse a `testcontainers` container with two different drivers?

How to reuse vue method for different form inputs

How to reuse a component if it should be slightly different?

How to reuse component with different tags using react