Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Choice Based Text Generation

Status
Not open for further replies.

nsarun

IS-IT--Management
Nov 5, 2003
6
US
Dear folks, Need help with this one. I am sorry for my naive question.

I have a 15 char text field (partnum). Which needs to be populated with a character string constructed based on user choice. each choice gives one or more characters that form the string. The final string looks something like 'A23-R67F22-M44T'. I am using Javascript. I guess I am thoroughly confused with the way JS handles char array and string and their interconversion. The function looks something like this.

function makePN(pos, cnt, obj){

var tmpprt=new Array(15); //make this global so it retains the value

for(i=0;i<cnt;i++)
tmpprt[pos+i]=obj.value;//??

document.pngen.partnum.value=tmpprt;

}

where pos is the position the char needs to be inserted. cnt is the no. of chars from there. obj is the object (such as radio, list box, checkbox etc.) that sent the value.

My thanks for your much appreciated help, in advance.
 
1. To make a variable global, declare it outside of a function, or do not use the var command.

2. You are unfamiliar with javascript arrays. In javascript, all arrays are dynamic. They have no fixed length. new Array(15) will return a new array with the first argument equal to 15, not an array having 15 positions.

3. This should probably be done with a string, not an array. To insert text into a string, use this:
Code:
if (pos == 0)
   myString = newText + myString;
else
{
   if (pos == myString.length()-1)
      myString += newText;
   else
      myString = myString.substring(0,pos) + newText + myString.substring(pos+1,myString.length());
}
myString is the full string, newText is the text to insert, and pos is the position to put it in.

Hope this helped.


--Chessbot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top