Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Remove Last Character from String

    Hello everyone. I'm new to JS, so please go easy on me! I'm working on a bit of code in MS Dynamics CRM. I built a form and copied code to generate a multi-select picklist from an option set. The code below does all of this beautifully, I just need to insert code to remove the last character--in this case, the last comma in a string. Can someone please tell me how to incorporate this into the code below? Thank you kindly for your help.

    Code:
    function OnLoad ()
    {
    // PL - the picklist attribute; PLV - used to save selected picklist values     
    var PL = crmForm.all.new_list;     //CREATE NEW PICKLIST
    var PLV = crmForm.all.new_text;  //CREATE NEW TEXT FIELD TO STORE STRING
     
    
    PL.style.display = "none";       //HIDES THE CONTROL
    PLV.style.display = "none";     //HIDES THE CONTROL
    
     
        // Create a DIV container    
        var addDiv = document.createElement("<div style='overflow-y:auto; height:135px; width:278px; border:1px #a1a5aa solid; background-color:#ffffff;' />");    
        PL.parentNode.appendChild(addDiv);    
        // Initialise checkbox controls    
     
    
        for( var i = 1; i < PL.options.length; i++ )    
        {    
            var pOption = PL.options[i];    
            if( !IsChecked1( pOption.text ) )   
            var addInput = document.createElement("<input type='checkbox' onclick='CallOnChangeEvent()' style='border:none; width:25px; align:left;' />" );
            else    
            var addInput = document.createElement("<input type='checkbox' onclick='CallOnChangeEvent()' checked='checked' style='border:none; width:25px; align:left;' />" );
            var addLabel = document.createElement( "<label />");    
            addLabel.innerText = pOption.text;    
    
    
            var addBr = document.createElement( "<br>"); //it's a 'br' flag    
    
    
            PL.nextSibling.appendChild(addInput);    
            PL.nextSibling.appendChild(addLabel);    
            PL.nextSibling.appendChild(addBr);
        }
     
    
       // Check if it is selected    
       function IsChecked( pText )
       {
       if(PLV.value != "")
          {
          var PLVT = PLV.value.split(", ");
          for( var i = 0; i < PLVT.length; i++ )
          {
             if( PLVT[i] == pText )
             return true;
          }
        }
        return false;
       }
    }
     
    
    function CallOnChangeEvent()
    {
          var PL = crmForm.all.new_list;  
          var PLV = crmForm.all.new_text;
          PLV.value = "";
          var getInput = PL.nextSibling.getElementsByTagName("input");
          for( var i = 0; i < getInput.length; i++ )
          {
             if( getInput[i].checked)
             {
                PLV.value += getInput[i].nextSibling.innerText + ", ";
             }
          }
     
    
         //MUST DO THIS TO TRIGGER A SAVE EVENT
         var Name = Xrm.Page.data.entity.attributes.get("new_text");
        Name.setValue(PLV.value);
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    That's not hard:
    Code:
        s = s.substring( 0, s.length - 1 );
    But there's and even easier way.

    I assume you are referring to your CallOnChangeEvent() function?

    Try this:
    Code:
    function CallOnChangeEvent()
    {
          var PL = crmForm.all.new_list;  
          var PLV = crmForm.all.new_text;
          var list = "";
          var getInput = PL.nextSibling.getElementsByTagName("input");
          for( var i = 0; i < getInput.length; i++ )
          {
             if( getInput[i].checked)
             {
                list += "," + getInput[i].nextSibling.innerText; // innerText not supported in all browsers!!!
             }
          }
          PLV.value = list.substring(1); // chop the character off the front, instead
    
          ...
    See? Easier.

    As an alternative, just don't put the first comma in the list:
    Code:
    function CallOnChangeEvent()
    {
          var PL = crmForm.all.new_list;  
          var PLV = crmForm.all.new_text;
          var list = "";
          var getInput = PL.nextSibling.getElementsByTagName("input");
          for( var i = 0; i < getInput.length; i++ )
          {
             if( getInput[i].checked)
             {
                if ( list != "" ) list += ",";
                list += getInput[i].nextSibling.innerText; 
             }
          }
          PLV.value = list; // nothing to drop now!
    
          ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Old Pedant, thanks for your quick reply. Yes, it's for the CallOnChangeEvent() function. I tried the second and third way, and neither worked. Where would the s = s.substring( 0, s.length - 1 ); code be entered?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    What does "neither worked" mean? In what way did they fail?

    Either one is better than removing the last character.

    But if you must:
    Code:
    function CallOnChangeEvent()
    {
          var PL = crmForm.all.new_list;  
          var PLV = crmForm.all.new_text;
          PLV.value = "";
          var getInput = PL.nextSibling.getElementsByTagName("input");
          for( var i = 0; i < getInput.length; i++ )
          {
             if( getInput[i].checked)
             {
                PLV.value += getInput[i].nextSibling.innerText + ", ";
             }
          }
          // new code:
          PLV.value = PLV.value.substring( 0, PLV.value.length - 2 );
    
          ...
    And I used - 2 instead of - 1 because you are ACTUALLY adding COMMA-SPACE each time through the loop, so you need to remove the last COMMA-SPACE (2 characters, not 1).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you saying I should replace:

    Code:
    PLV.value += getInput[i].nextSibling.innerText + ", ";
    with:

    Code:
    PLV.value = PLV.value.substring( 0, PLV.value.length - 2 );
    ?

    Sorry, I'm really new to JS and the syntax. I'm trying to understand how replacing it will maintain the comma-space for all the other strings. Or am I supposed to add the new code right below it?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    No. I am saying you should *ADD* the code I showed as "new code":
    Code:
          // new code:
          PLV.value = PLV.value.substring( 0, PLV.value.length - 2 );
    in the place where I showed it.

    That's why I called it "new code" instead of "replacement code" and why I showed where it goes in your existing code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •