Skip to main content
Participant
June 30, 2022
Question

Consecutive letters text field

  • June 30, 2022
  • 1 reply
  • 1423 views

Trying to create multiple text fields with consecutive letting based on user input of text box 1. 

For example

If user enters "A" in text box 1, text box 2 will auto populate "B" and text box 3 will have "C",  and so fourth. At the end of the alphabet, after "Z", it would auto populate "AA".

Can someone help me out please, I've been searching for hours with no luck.

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
June 30, 2022

Doing it up to "Z" is not too difficult, but then it becomes a bit tricky. How many such fields do you have?

Participant
June 30, 2022

@try67 It differs depening on the day, sometimes I'll have 5 fields, sometimes it can go up to 180+. If you can show me how to do it up to "Z" I can write in the other letter after it doubles up.

 

Thank you so much

try67
Community Expert
Community Expert
June 30, 2022

This is not my code, I found it online (and tweaked it a bit), but it seems to work pretty well:

 

function colName(n) {
	var ordA = 'a'.charCodeAt(0);
	var ordZ = 'z'.charCodeAt(0);
	var len = ordZ - ordA + 1;
  
	var s = "";
	while(n >= 0) {
		s = String.fromCharCode(n % len + ordA) + s;
		n = Math.floor(n / len) - 1;
	}
	return s.toUpperCase();
}