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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:45:07+00:00 2026-05-13T14:45:07+00:00

i have a bunch of code where i dynamically create dropdown boxes in javascript.

  • 0

i have a bunch of code where i dynamically create dropdown boxes in javascript. To prepulate the list of dropdown items, i often have code similar to below where i “inject” in the array of data items from my model so i have <%= myHTMLhelper.ShowArray(Model.List ofItems %> code inside of a javascript function.

This works perfectly except that i now have similar code that i want to reuse across files as i dont want to copy and paste code. The issue is that normally i would move my javascript code into a seperate .js file and import that into my multiple views but in this case i can’t as that <% %> code wont work inside of a .js file.

Any suggestions on how can avoid copy and paste solution here ?

$(".dependencyAddButton").click(function() {

                var tblID = $(this).prev("table").attr("id");

                var lastRow = GetLastRow(tblID);

                var existingItems = GetExistingItems('#' + tblID);

                <%= myHTMLhelper.ShowArray(Model.AllApplications, "components", "componentIds") %>
  • 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-13T14:45:07+00:00Added an answer on May 13, 2026 at 2:45 pm

    What you want to do is create a jQuery plugin from your script and pass all server-side information into the plugin as a parameter. Using your example, you would have the following plugin defined in an external file:

    jQuery.fn.dropdown = function(array) {
        var tableId = $(this).prev('table').attr('id');
        var lastRow = GetLastRow(tblId);
        var existingItems = GetExistingItems('#' + tblId);
        var dropDownItems = array;
        // DO STUFF WITH THE ARRAY
    };
    

    You would then call it from your view:

    $('.dependencyAddButton').dropdown(<%= myHTMLhelper.ShowArray(Model.AllApplications, "components", "componentIds") %>);
    

    This is a pretty common technique that you will find used whenever dynamic information is required. It might be a URL or some other configuration property.

    Please note that this was a jQuery plugin written off the top of my head. It doesn’t necessarily follow current best practices for writing plugins; it might not even work. It is simply an example of the technique that you can use to handle issues like you were having. Take a look at one of the following links for more information on writing jQuery plugins:

    • http://docs.jquery.com/Plugins/Authoring
    • http://blog.jeremymartin.name/2008/02/building-your-first-jquery-plugin-that.html
    • http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Plug_me:_Writing_your_own_plugins
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just considering one word, here is an O(n log n)… May 13, 2026 at 7:31 pm
  • Editorial Team
    Editorial Team added an answer That's all I do when I move slns. It's never… May 13, 2026 at 7:31 pm
  • Editorial Team
    Editorial Team added an answer To the employee class. The team class will have a… May 13, 2026 at 7:31 pm

Related Questions

I have created a WPF app where I dynamicly build XAML elements using c#
The question How can I monkey-patch an instance method in Perl? got me thinking.
I have html code like this <tr class=odd> <td><b>Reason for Termination:</b></td> <td>this has bunch
Same intro as my last question: I am re-writing and/or consolidating a bunch of
I'm working on a Python web application in which I have some small modules

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.