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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:10:29+00:00 2026-06-09T01:10:29+00:00

I have a view and a loop within which a partial view is rendered.

  • 0

I have a view and a loop within which a partial view is rendered. With the partial view I have a multi select listbox. So based on count of a item in the loop there could (n) number of listboxes.

My goal is to get all the selected items if any from the first listbox and pre-select them in the remainder of the listboxes. I am not trying to append to the remaining listboxes but just whatever is selected in the first, i would select in the remaining. all the listboxes will have the same items in them.

I am facing diffculty to find the selected indexes or items only from the first one and then i will do the preselection in the remaining, if i could just get the indexes of the selected items in the first one would be helpful. It gives selected items from all the listboxes. please help:

listbox decleration within the partial view

 @Html.ListBoxFor(model => model.ServiceTypes,
           new MultiSelectList(RunLog.Domain.Lists.GlobalList.PartsServiceTypes(), "ID", "Name"),
           new {
               style = "width: 200px; height: 80px;",
               id = "lstbox",
               name = "listbox"
           })

button which renders the function

<input id="button" type="button" class="art" onclick="dosomething()" name="broadcast"  value="+" />

JS Function:

function broadcast() {
    //var element = $('select[multiple]'); This gives me access of all listboxes
    // var firstListBoxSelected = $('select[multiple][1] option:selected').text(); t
}
  • 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-09T01:10:31+00:00Added an answer on June 9, 2026 at 1:10 am

    In your example, you gave your listbox the id of “lstbox”. You can use this to find the “list box” using jQuery:

    var box = $('#lstbox'); // or $('select[multiple]:first') for just the first one
    

    From there, you can modify the code to filter down to just the selected options:

    var selected = $('#lstbox option:selected');
    

    And finally, to get the indexes, we change it again and add just a couple more lines of code:

    var selectedIndices = []; // create an empty array
    $.each($('#lstbox option:selected'), function(index, value) { // loop over each option
        selectedIndices.push(index); // add the index to the array
    });
    

    Or a slightly different approach, taking :selected out of your selector and instead manually checking whether the element is selected (might be a hair better in terms of performance):

    var selectedIndices = [];
    $.each($('#lstbox option'), function(index, value) {
        if (this.selected) { // 'this' is the current DOM element, same as 'value'
            selectedIndices.push(index);
        }
    });
    

    Then you can use selectedIndices to pre-select the remaining ones, but first we have to find them:

    var otherBoxes = $('select[multiple]:not(:first)'); // not the first one
    // or
    var otherBoxes = $('select[multiple]:gt(0)'); // greater than the 0th one
    

    And then change their selected options:

    var numSelected = selectedIndices.length;
    $.each(otherBoxes, function() {
        for (int i = 0; i < numSelected; i++) {
            this.options[selectedIndices[i]].selected = true;
        }
    });
    

    EDIT: Working jsFiddle example

    My jsFiddle solution looks like this (I combined the loops so you only have to iterate the select elements once):

    $(function() {
        var selectedIndices = [];
        $.each($('select[multiple]'), function(sIndex, sValue) {
            if (sIndex == 0) {
                $.each(this.options, function (oIndex, oValue) {
                    if (this.selected)
                        selectedIndices.push(oIndex);
                });
            } else {
                for (var i = 0; i < selectedIndices.length; i++) {
                    this.options[selectedIndices[i]].selected = true;
                }    
            } 
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a loop which renders elements within a partial view. The elements
There are many functions within the code I am maintaining which have what could
I have a razor view with a BeginForm command and a loop within that
I have a view called volunteer_edit. Within volunteer_edit, I use jQuery to loop through
In my view I render a partial view within a loop. The problem I
I have a tabled view in a while loop, where a user can view
So I have a for loop in my View that is supposed to render
I have a for loop that creates multiple UIImageViews that I add to self.view
I have view on which I defined a custom tagName. I need to be
i have view like 'home/details/5', it can be access by anonymous user. but there

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.