...

View Full Version : Remove Last Character from String



jaspla
06-18-2012, 08:55 PM
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.



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);
}

Old Pedant
06-18-2012, 09:20 PM
That's not hard:


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:


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:


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!

...

jaspla
06-19-2012, 03:16 PM
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?

Old Pedant
06-19-2012, 09:58 PM
What does "neither worked" mean? In what way did they fail?

Either one is better than removing the last character.

But if you must:


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).

jaspla
06-22-2012, 09:28 PM
Are you saying I should replace:


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

with:


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?

Old Pedant
06-22-2012, 09:32 PM
No. I am saying you should *ADD* the code I showed as "new 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum