473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unselecting options in a div (JavaScript)

2
Thank you in advance for any assistance!

We are currently trying to implement a javascript solution for CRM2011. There are 2 libraries involved. The first generates a div container and presents values from a dropdown list (option set in CRM) so the user can select multiple values. The code then saves the selected values to a separate field as delimited text.

We have a requirement to allow for the values to be cleared. Specifically, if a function is called in a separate library, resulting in the list being hidden, then the selected values will also clear. As it stands, the function in the second library will run (hiding the multiple choice section), but the values are not cleared.

Here are the 2 libraries in question. The second includes codes from another bytes.com post (http://bytes.com/topic/javascript/answers/712814-uncheck-checkboxes-within-single-div) and is what we've tried to make word in our solution.

Library that creates the div container:

Expand|Select|Wrap|Line Numbers
  1. ///<reference path="..\IntelliSense\XrmPage-vsdoc.js"/>
  2. if (typeof (OptionSetToMultiSelect) == "undefined")
  3. { OptionSetToMultiSelect = {}; }
  4. OptionSetToMultiSelect.Functions = {
  5.  
  6.     ToMultiSelect: function (var_sc_optionset, var_sc_optionsetvalue, required, columns, othercontrol, othercontrollabel) {
  7.  
  8.         if (othercontrol != null) {
  9.             var MyControl = Xrm.Page.getControl(othercontrol);
  10.             var MyControlValue = Xrm.Page.getAttribute(othercontrol).getValue();
  11.             if (MyControlValue != null) {
  12.                 MyControl.setVisible(true);
  13.             }
  14.             else {
  15.                 MyControl.setVisible(false);
  16.             }
  17.         }
  18.  
  19.         var OS = document.getElementById(var_sc_optionset);
  20.         var OSV = document.getElementById(var_sc_optionsetvalue);
  21.  
  22.         if (OS != null && OSV != null) {
  23.             if (columns == null)
  24.                 columns = 1;
  25.             OS.style.display = "none";
  26.             if (required) {
  27.                 OS.parentNode.parentNode.firstChild.appendChild(document.createElement('<IMG class=ms-crm-ImageStrip-frm_required alt=Required src="/_imgs/imagestrips/transparent_spacer.gif?ver=-137896071">'));
  28.             }
  29.             OSV.parentNode.parentNode.style.display = 'none'
  30.  
  31.             // Create a DIV container
  32.             var divHight = (OS.options.length < 4 ? 80 : (OS.options.length - 1 * 20));
  33.             var addDiv = document.createElement("<div id='div1' style='overflow-y:auto; height:" + divHight + "px; border:1px #cecece solid;' />");
  34.             OS.parentNode.appendChild(addDiv);
  35.  
  36.             var count = 0;
  37.             var totalPerColumn = (OS.options.length - 1) / columns;
  38.             OS.nextSibling.appendChild(document.createElement("<div id='div2' style='width:90px;float:left;' />"))
  39.  
  40.             // Initialise checkbox controls
  41.             for (var i = 1; i < OS.options.length; i++) {
  42.                 var pOption = OS.options[i];
  43.  
  44.                 if (!OptionSetToMultiSelect.Functions.IsChecked(pOption.text, OS, OSV)) {
  45.                     var addInput = document.createElement("<input type='checkbox' id='input1' style='border:none; width:25px; align:left;' value='" + pOption.text + "' />");
  46.                 }
  47.                 else {
  48.                     var addInput = document.createElement("<input type='checkbox' id='input2' checked='checked' style='border:none; width:25px; align:left;' value='" + pOption.text + "' />");
  49.                 }
  50.  
  51.  
  52.                 addInput.onclick = function () {
  53.  
  54.                     var valuesField = Xrm.Page.getAttribute(var_sc_optionsetvalue);
  55.                     var customFields = OS.nextSibling.getElementsByTagName("input");
  56.                     var values = "";
  57.  
  58.                     for (var j = 0; j < customFields.length; j++) {
  59.                         if (customFields[j].checked) {
  60.                             values += customFields[j].value;
  61.                             values += ";"
  62.  
  63.                             if (othercontrol != null) {
  64.                                 if (customFields[j].value == othercontrollabel) {
  65.                                     var MyControl = Xrm.Page.getControl(othercontrol);
  66.                                     var MyControlAttribute = Xrm.Page.getAttribute(othercontrol);
  67.                                     MyControl.setVisible(true);
  68.                                     MyControlAttribute.setRequiredLevel("required");
  69.                                 }
  70.                             }
  71.  
  72.                         }
  73.                         else {
  74.                             if (othercontrol != null) {
  75.  
  76.                                 var MyControl = Xrm.Page.getControl(othercontrol);
  77.                                 var MyControlAttribute = Xrm.Page.getAttribute(othercontrol);
  78.                                 MyControl.setVisible(false);
  79.                                 MyControlAttribute.setRequiredLevel("none");
  80.                                 Xrm.Page.getAttribute(othercontrol).setValue(null);
  81.                             }
  82.                         }
  83.                     }
  84.  
  85.                     values = values.substring(0, values.length - 1);
  86.                     valuesField.setValue(values);
  87.                 };
  88.  
  89.                 var label = document.createElement("<label style='width:150px' />");
  90.                 label.appendChild(addInput)
  91.                 var labelText = document.createElement("<span style='white-space:nowrap;'/>");
  92.                 labelText.innerText = pOption.text;
  93.                 label.appendChild(labelText)
  94.  
  95.                 if (count >= totalPerColumn) {
  96.                     OS.nextSibling.appendChild(document.createElement("<div style='width:90px;float:left;' />"))
  97.                     count = 0;
  98.                 }
  99.  
  100.                 OS.nextSibling.lastChild.appendChild(label);
  101.                 count++;
  102.             }
  103.  
  104.             if (required) {
  105.                 OptionSetToMultiSelect.Functions.OptionSetToMultiSelectRequiredFields.push(var_sc_optionsetvalue);
  106.             }
  107.         }
  108.     },
  109.  
  110.     // Check if it is selected
  111.     IsChecked: function (pText, OS, OSV) {
  112.         if (OSV.value != "") {
  113.             var OSVT = OSV.value.split(";");
  114.             for (var i = 0; i < OSVT.length; i++) {
  115.                 if (OSVT[i] == pText)
  116.                     return true;
  117.             }
  118.         }
  119.         return false;
  120.     },
  121.  
  122.     // Save the selected text, this field can also be used in Advanced Find
  123.     OnSave: function (var_sc_optionsetvalue, var_sc_optionset, required) {
  124.         //save value
  125.         control = Xrm.Page.getControl(var_sc_optionsetvalue);
  126.         valueControl = Xrm.Page.getControl(var_sc_optionsetvalue).getAttribute()
  127.  
  128.         if (required && valueControl.getValue() == null) {
  129.             alert("You must provide a value for " + var_sc_optionset);
  130.             event.returnValue = false;
  131.             return false;
  132.         }
  133.     },
  134.     CheckAllMultiCheckboxWithLayersRequiredFields: function () {
  135.  
  136.  
  137.         for (var i = 0; i < OptionSetToMultiSelect.Functions.OptionSetToMultiSelectRequiredFields.length; i++) {
  138.             var valueControl = Xrm.Page.getControl(OptionSetToMultiSelect.Functions.OptionSetToMultiSelectRequiredFields[i]);
  139.             var isDisabled = valueControl.getDisabled();
  140.             if (valueControl.getAttribute().getValue() == null && !isDisabled) {
  141.                 alert("You must provide a value for " + valueControl.getLabel().replace("Values", ""));
  142.                 OptionSetToMultiSelect.Functions.OptionSetToMultiSelectValidationTriggered = true;
  143.                 event.returnValue = false;
  144.                 return false;
  145.             }
  146.         }
  147.     },
  148.     OptionSetToMultiSelectRequiredFields: new Array(),
  149.     OptionSetToMultiSelectValidationTriggered: false
  150. }
  151.  
Library that contains the function to show/hide the section with the div container, and ALSO attempt to clear the values. (Please note, I realize the show/hide is always showing - I wanted to see if it was clearing.)

Expand|Select|Wrap|Line Numbers
  1. // OnLoad
  2.  
  3. function Form_onload() {
  4.  
  5. }
  6.  
  7. function testTrigger_onchange() {
  8.  
  9.     var triggerAttribute = Xrm.Page.getAttribute("me_pickanoptiontotriggermultiselect");
  10.     var trigger = triggerAttribute.getValue();
  11.  
  12.     if (trigger == 134320002) {
  13.  
  14.         Xrm.Page.ui.tabs.get("section_1").sections.get("item_3").setVisible(true);
  15.         alert("Toggled On");
  16.  
  17.     } else {
  18.  
  19.         Xrm.Page.ui.tabs.get("section_1").sections.get("item_3").setVisible(true);
  20.         alert("Toggled Off");
  21.  
  22.         function unselect(div2) {
  23.             var inputs = document.getElementById(div2).getElementsByTagName(input2);
  24.             for (var i = 0; i < inputs.length; i++) {
  25.                 var obj = inputs[i];
  26.                 if (inputs[i].type == "checkbox") {
  27.                     inputs[i].checked = false;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         alert("Ran Clear");
  33.  
  34.     }
  35.  
  36. }
  37.  
Any thoughts on how to go about clearing the values from the div?
Oct 25 '12 #1
1 2367
gprisk
2
Additional information from a coworker:

http://social.msdn.microsoft.com/For...2-c9a564368633
Oct 25 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: beguigne | last post by:
Below is a snippet of a crude date picking routine for a form. I am a novice at this so, I am hitting my head at why when I select the day, the onChange event gives me a blank. What am I missing?...
10
by: SueB | last post by:
Hi. Is there a way to "unselect" items in a listbox, programmatically? Here's what I want to do (and I've been trying to to this while debugging the form, but I've been unsuccessful) ... I...
0
by: Ramesh Chawla | last post by:
Hi, I am making a table in ASP.NET at the runtime and loading the data in the left and the right most colums and in between there are 9 columns on which data from either of the columns(left...
8
by: Matt Kruse | last post by:
Using the latest version of Opera 8.5, this test case: -------------------------------------------------------- <select name="sel" multiple="true" size="3"> <option value="option1"...
4
by: divina11 | last post by:
I'm trying to retrieve the options below, depending on the one the user selects, I'm bulliding a Calculator Program where user inputs 2 values via input box and then selects an operation: Code:...
5
by: Doogie | last post by:
Hi, I have a listbox set up so users can select multiple choices. I want to limit them so they can choose no more than 4 at a time. I have set up javascript code to do the check on the number of...
0
by: atencorps | last post by:
Hello I have the following code but need some help on it. The idea of the code is the main sections ie Service Management are viewable when the page is loaded and by clicking on the main...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.