JavaScript Add selected options value into an array in javascript options selected value javascript,options selected value jquery,options selected

I have 2 multiple select boxes where I select some of the options and add it to the other select box. When i click on save, the selected option values should be stored into one array and passed to the home controller. when i put alert i get the proper data but I get the below error in firebug console which i'm not able to solve it.Plse help me in this regard. here is the error and code.

for (var i = 0; i <= PairedSelectBox.options.length; i++) {
    selectedClientChips.push(PairedSelectBox.options[i].value);
    alert("selectedClientChips == " + selectedClientChips);
}

I get the error in console like this

TypeError: PairedSelectBox.options[i] is undefined
selectedClientChips.push(PairedSelectBox.options[i].value);

This is the html code:

<table>
    <tr>
        <td>
            <label style="font-size: medium;">Client Chip details</label>
        </td>
    </tr>

    <tr>
        <td>
            <label>Search</label>
            <input type="text" id="searchId">
            <input type="button" value="Go" onclick="getClientChipDetails();"></td>
    </tr>
    <tr>
        <td>
            <label style="font-size: small;">Client-Chip data</label>
        </td>
        <td></td>
        <td>
            <label style="font-size: small">Selected Client-Chip data</label>
        </td>
    </tr>
    <tr>
        <td>
            <select id="MasterSelectBox" name="MasterSelectBox" size="10" multiple="multiple"></select>
        </td>
        <td>
            <button id="btnAdd">> </button>
            <br>
            <button id="btnRemove">< </button>
        </td>

        <td>
            <select id="PairedSelectBox" name="PairedSelectBox" size="10" multiple="multiple">
            </select>
        </td>
    </tr>

</table>

This is the script:

$(document).ready(function () {

    $('#MasterSelectBox').pairMaster();
    $('#btnAdd').click(function () {
        $('#MasterSelectBox').addSelected('#PairedSelectBox');
    });
    $('#btnRemove').click(function () {
        $('#PairedSelectBox').removeSelected('#MasterSelectBox');
    });
});
Answer:1

Try this. I modify your code.

HTML

<table>
    <tr><td>Master select box</td><td>Paired select box</td></tr>
    <tr>
        <td>
            <select id="MasterSelectBox" name="MasterSelectBox" size="10" multiple="multiple">
                <option value="volvo">Volvo</option>
                <option value="saab">Saab</option>
                <option value="mercedes">Mercedes</option>
                <option value="audi">Audi</option>
            </select>
        </td>
        <td>
            <button id="btnAdd"> > </button><br>
            <button id="btnRemove"> < </button>
        </td>
        <td>
            <select id="PairedSelectBox" name="PairedSelectBox" size="10" multiple="multiple"></select>
        </td>        
    </tr>
</table>
<button id="save">Save</button>
<p id="output"></p>  

Javascript

$(document).ready( function(){ 
    $('#MasterSelectBox').pairMaster();
    $('#btnAdd').click(function(){
        $('#MasterSelectBox').addSelected('#PairedSelectBox');
    });
    $('#btnRemove').click(function(){
        $('#PairedSelectBox').removeSelected('#MasterSelectBox');
    });
    $( "#save" ).click( function( e ) {
        $( "#output" ).empty();
        $.each( $( "#PairedSelectBox" ).val(), function( i, v ) {
            $( "#output" ).append( $( "<span> " + v + " </span>" ) );
        } );
    } );
});

P.S. and use rawgit.com links to libraries, not raw.githubusercontent.com to attach scripts to jsfiddle.

Answer:2

I'm trying to create some isolation in javascript using the module pattern. but when i run this code: var mymap = {}; mymap['230'] = (function () { privatePageLoad = function(nid, page){ ...

I'm trying to create some isolation in javascript using the module pattern. but when i run this code: var mymap = {}; mymap['230'] = (function () { privatePageLoad = function(nid, page){ ...

  1. javascript module pattern es6
  2. javascript module pattern example
  3. javascript module pattern with jquery
  4. javascript module pattern vs class
  5. javascript module pattern w3schools
  6. javascript module pattern vs revealing module pattern
  7. javascript module pattern inheritance
  8. javascript module pattern constructor
  9. javascript module pattern mdn
  10. javascript module pattern private variables
  11. javascript module pattern vs prototype
  12. javascript module pattern import
  13. javascript module pattern document ready
  14. javascript module pattern private function
  15. javascript module pattern event handler
  16. javascript module pattern vs object literal
  17. javascript module pattern with parameters
  18. javascript module pattern tutorial

I need suggestions about writing better code in Revealing Module Pattern way. I have followed tutorial http://weblogs.asp.net/dwahlin/archive/2011/09/05/creating-multiple-javascript-objects-when-using-...

I need suggestions about writing better code in Revealing Module Pattern way. I have followed tutorial http://weblogs.asp.net/dwahlin/archive/2011/09/05/creating-multiple-javascript-objects-when-using-...

  1. javascript revealing module pattern
  2. javascript revealing pattern
  3. javascript revealing module pattern constructor
  4. javascript revealing prototype pattern
  5. javascript revealing module pattern with parameters
  6. javascript revealing module
  7. javascript revealing module pattern multiple instances
  8. javascript revealing module pattern es6
  9. javascript revealing module pattern vs prototype
  10. javascript revealing module pattern inheritance
  11. javascript revealing module pattern nested namespace
  12. javascript revealing module pattern events
  13. javascript revealing module pattern tutorial
  14. javascript revealing module pattern public variables
  15. javascript revealing
  16. javascript self revealing module pattern
  17. javascript class revealing pattern
  18. javascript module vs revealing module
  19. revealing module pattern javascript
  20. revealing object pattern javascript

I need to use addEventListener for automatically updating the values typed into different input text (actually 4 input tags) Here's the HTML code : <td class="col-md-4"> <label for="...

I need to use addEventListener for automatically updating the values typed into different input text (actually 4 input tags) Here's the HTML code : <td class="col-md-4"> <label for="...

  1. javascript addeventlistener with parameters
  2. javascript addeventlistener with this
  3. javascript addeventlistener with event
  4. javascript addeventlistener with params
  5. js addeventlistener with parameter
  6. js addeventlistener with this
  7. js addeventlistener with param
  8. js addeventlistener with arguments
  9. javascript addeventlistener to class
  10. javascript addeventlistener to dynamically created element
  11. javascript addeventlistener to multiple elements
  12. javascript addeventlistener to element
  13. javascript addeventlistener to button
  14. javascript addeventlistener to div
  15. javascript addeventlistener to all elements
  16. javascript addeventlistener to all buttons
  17. javascript addeventlistener to object
  18. javascript addeventlistener in loop
  19. javascript addeventlistener to input
  20. javascript addeventlistener to list of elements

I've got an event listener in my Collection which listens for the 'add' event, which only seems to fire when the sample data I add in my router is added, but not when I add more models in my view, the ...

I've got an event listener in my Collection which listens for the 'add' event, which only seems to fire when the sample data I add in my router is added, but not when I add more models in my view, the ...

  1. backbone collection fetch
  2. backbone collection reset
  3. backbone collection add
  4. backbone collection filter
  5. backbone collection sort
  6. backbone collection events
  7. backbone collection each
  8. backbone collection set
  9. backbone collection get
  10. backbone collection comparator
  11. backbone collection length
  12. backbone collection to array
  13. backbone collection find by id
  14. backbone collection save
  15. backbone collection extend
  16. backbone collection foreach
  17. backbone collection parse
  18. backbone collection remove
  19. backbone collection where
  20. backbone collection first