locked
Make the related value true in javascript based upon an array RRS feed

  • Question

  • User1793652459 posted

    Hello,

    This javascript code was working fine when users could only select one value like 1, or 2, or 3 or 4.     

    switch (StateID) {
    case "1":
            vm.checkValue1 = true;        
    break;                  
    case "2":    
     vm.checkValue2 = true;
    break;                   
    case "3":
    vm.checkValue3 = true;                             
    break;                     
    case "4":                           
    vm.checkValue4 = true;                           
    break;                       
    default:
    vm.checkValue5 = true;                           
    break;
       }

    Now this field has become multivalues and they can select  values like, (1,2) or (2,3,4) or (2,4), basically any combination of 1-4 numbers. What change do I have to make to this code so the appropriate checkValues(X)  become true.  I don't think it would be a switch case statement anymore, would it be foreach or angular.foreach?  Can someone please provide an example.

    Thanks

    John

    Saturday, June 23, 2018 9:33 AM

Answers

  • User-474980206 posted
       var e = document.getElementById("myList");
       for (var i=0; i < e.options.length; ++i) {
           var o = e.options[i];
           vm["checkValue" + o.value] = o.selected;
       }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 23, 2018 10:06 PM