need help with javascript program

ibex333

Diamond Member
Mar 26, 2005
4,092
123
106
If you just copy paste the code and save as html document, you'll see that this displays buttons from A to Z and buttons for backspace and clear, which can be typed into the input box.

I want to separate the buttons such that I only have 9 per line. I have no idea how to do that, and I could really use some help.. Thanks!


PS: Off course, the last line will not have 9 buttons, and that is fine. It will have whatever is left.


Code:
<!DOCTYPE html>
<html>
<head>
	<title>Typewriter</title>
	<script>
		function backSpace() {
			var t = document.typing.disp.value;
			var n = t.substr(0, t.length - 1);
			document.typing.disp.value = n;
		}
	</script>
</head>
<body>
	<form id="typing" name="typing">
		<input type="text" id="disp" name="disp" size="50" value="" disabled="disabled" />
		<script type="text/javascript">
		for (k = 65; k <= 90; k++) {
			letter = String.fromCharCode(k);
			document.write("<input type=\"button\" value=\""+letter+"\" onclick=\"document.typing.disp.value += '"+letter+"';\" />");
		}
		</script>
		<input type="button" value="CLR" onclick="document.typing.disp.value = '';" />
		<input type="button" value="<--" onclick="backSpace();" />
	</form>
</body>
</html>
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
If you just copy paste the code and save as html document, you'll see that this displays buttons from A to Z and buttons for backspace and clear, which can be typed into the input box.

I want to separate the buttons such that I only have 9 per line. I have no idea how to do that, and I could really use some help.. Thanks!


PS: Off course, the last line will not have 9 buttons, and that is fine. It will have whatever is left.


Code:
<!DOCTYPE html>
<html>
<head>
	<title>Typewriter</title>
	<script>
		function backSpace() {
			var t = document.typing.disp.value;
			var n = t.substr(0, t.length - 1);
			document.typing.disp.value = n;
		}
	</script>
</head>
<body>
	<form id="typing" name="typing">
		<input type="text" id="disp" name="disp" size="50" value="" disabled="disabled" />
		<script type="text/javascript">
		for (k = 65; k <= 90; k++) {
			letter = String.fromCharCode(k);
			document.write("<input type=\"button\" value=\""+letter+"\" onclick=\"document.typing.disp.value += '"+letter+"';\" />");
		}
		</script>
		<input type="button" value="CLR" onclick="document.typing.disp.value = '';" />
		<input type="button" value="<--" onclick="backSpace();" />
	</form>
</body>
</html>

you can add these lines after the for loop
Code:
if (k==73)
document.write("<br>");
else if(k==82)
document.write("<br>");

Very crude but will get the job done.You can use the "||" operator instead.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
you can add these lines after the for loop
Code:
if (k==73)
document.write("<br>");
else if(k==82)
document.write("<br>");

Very crude but will get the job done.You can use the "||" operator instead.

Don't do any JS myself, does the MOD operator work in JS? You can do something like if(k MOD 9 == 0) then insert the BR. If you don't want the breaks in a funky spot, start a counter variable to use in your MOD statement.

EDIT:

From what I remember, JS is a lot like C++. The MOD operator in C++ is %.

EDIT EDIT:

Since you posted the code, I could play with it. MOD function works! Here you go:

Code:
<!DOCTYPE html>
<html>
<head>
	<title>Typewriter</title>
	<script>
		function backSpace() {
			var t = document.typing.disp.value;
			var n = t.substr(0, t.length - 1);
			document.typing.disp.value = n;
		}
	</script>
</head>
<body>
	<form id="typing" name="typing">
		<input type="text" id="disp" name="disp" size="50" value="" disabled="disabled" />
		<script type="text/javascript">
		var cnt = 0;
		for (k = 65; k <= 90; k++) {
			if (cnt % 9 == 0)
			document.write("<br/>");
			cnt++;
			letter = String.fromCharCode(k);
			document.write("<input type=\"button\" value=\""+letter+"\" onclick=\"document.typing.disp.value += '"+letter+"';\" />");

		}
		</script>
		<input type="button" value="CLR" onclick="document.typing.disp.value = '';" />
		<input type="button" value="<--" onclick="backSpace();" />
	</form>
</body>
</html>

EDIT * 3:

Just to explain what the MOD operator does is it takes a number, divides it by a specified number (in this case, 9) and tells you if there is a remainder. If you do 0 MOD any number your answer will be 0. But for example if you do 9 MOD 9, you get 0 as a remainder so the MOD function returns a 0. It's very useful for doing things every X number of times (also commonly used to determine if a number is odd or even: 9 mod 2 is 1, therefor 9 is odd; 12 mod 2 is 0, therefor the number is even).
 
Last edited:

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
Don't do any JS myself, does the MOD operator work in JS? You can do something like if(k MOD 9 == 0) then insert the BR. If you don't want the breaks in a funky spot, start a counter variable to use in your MOD statement.

EDIT:

From what I remember, JS is a lot like C++. The MOD operator in C++ is %.

"||" is actually the logical OR operator in JS . I mentioned that if op doesn't like "if...elseif" branch.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
"||" is actually the logical OR operator in JS . I mentioned that if op doesn't like "if...elseif" branch.

Yes I know that, thanks though

EDIT:

To be clear, in this scenario if you know you will only need 2 breaks, the simple OR statement is the best choice. I've listed a method which will allow you to expand the character options and not have to concern yourself with where to place the breaks. Also I feel it helps you learn the power of learning the language.

In this scenario, I would use Jaydip's approach. I listed mine for other possible uses in the future (and to see if I could get something to work in JS )

The best code for this small example is:

Code:
if (k==65 || k==74 || k==83)
document.write("<br/>");
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Or you could avoid javascript altogether, write the buttons into a div, and set the width of the div as an appropriate multiple of the button width. The buttons will wrap naturally, and ultimately this is a more flexible approach to use in the future, where you might have to deal with different types of devices.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Or you could avoid javascript altogether, write the buttons into a div, and set the width of the div as an appropriate multiple of the button width. The buttons will wrap naturally, and ultimately this is a more flexible approach to use in the future, where you might have to deal with different types of devices.

Example? I've done a few websites but I've not fully grasped DIVs even though I use them.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I read the OP as requiring the buttons to wrap to a new line after nine instances. This is achieved by setting the div width appropriately. I don't see any real need for hard breaks.

Gotcha. Makes sense now. The width will force the buttons to wrap.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I guess this isn't the favored solution, but heres what I did without reading the thread:

Code:
	var offset = 65;
		
		 document.write("<br />");
		
		for (k = 0; k <= 25; k++) {
			
			letter = String.fromCharCode(k + offset);
			
			document.write("<input type=\"button\" value=\""+letter+"\" onclick=\"document.typing.disp.value += '"+letter+"';\" />");
						
			if(k > 1 && k % 9 == 0)
			{
			  document.write("<br />");
			}
		}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |