Hi all, this is my first thread here, but I've read your forum a lot as unregistered user. I've searched the web for a complete Javascript or some hint, but I didn't find an interesting script or article to implement what I've thought.
There's a way to write text or number from the keyboard directly in a:

<input type="text" name="display" value="0" maxlength="10" disabled="disabled">

Recommended Answers

All 12 Replies

I do not know whether I understood you correctly.
Try removing disabled attribute

<input type="text" name="display" value="0" maxlength="10">

Sorry for my short infos about my request.
Removing "disable" I'm able to write in the field, but I was meaning with the "disable" on how to capture the keystroke pressed from an user and automatically write it in that field.

The example is the Windows calculator, you don't need to put the cursor inside the field, just type what you want and it automatically will grab the key pressed.

this is a simple way of automatically focusing on the input so you don't have to click it...

<body onLoad="document.getElementById('yourinput').focus()">
commented: Awesome hint to fix my problem +0

Ok :)

Try using onkeypress if you are not willing to use jQuery.

First set an id value for the text field

<input type="text" id="display" name="display" value="0" maxlength="10" disabled="disabled">

Since your text field is disabled, you can not capture any key event from it. So try adding it to the document. Then use the event object to manipulate the pressed key according to your requirement.
I just appended each character to the end in the following example.

<script type="text/javascript">		
	function updateText(e)
	{
		var evtobj=window.event? event : e
		var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
		var actualkey=String.fromCharCode(unicode)
		document.getElementById("display").value += actualkey;
	}
	document.onkeypress = updateText;
</script>
commented: Really helpful, niranga +1 +0

Ahh damned me, thank you very much was really easy to do. I just forgot to things:
1. give the "id" name to the function in the Javascript (I gave the name instead)
2. the onLoad.

Really thank you guys.

Welcome :)

Sorry if I'm back, but seems that this isn't a good day for coding.

Using that great function I can develop my idea, but if I want to check also the pressed buttons and exclude some?

if ((actualkey > 8 || actualkey < 13) && (actualkey > 16 || actualkey < 48) && (actualkey > 57 || actualkey < 93) && actualkey > 111) {
		alert("Letter");
	} else if ((actualkey >= 48 || actualkey <= 57) && (actualkey == 107 /*add*/ || actualkey == 109 /*subtract*/ || actualkey == 106 /*multiply*/ || actualkey == 111 /*divide*/)) {
		alert("Number or Simbol (+ - * /)");
		document.getElementById("screen").value += actualkey;
	} else if (actualkey == 13) {
		alert("Print");
		document.getElementById("screen").value == eval(document.calculator.display.value);
	}

What I'm doing wrong?
It doesn't print me nothing also if I jump on the keyboard.

Code completely rewritten and problem Solved! :D

Good :)
I was going to ask you to use the variable "unicode" instead of "actualkey" in the above code :), because "actualkey" holds the char and "unicode" holds an integer which can be compared as you have done above..

Yes, I see. But I've added other things so I rewrote the entire function, but I have to thank you to have fixed my initial problem :D

Q1) Write a program that input multiple numbers from the user. And the first number entered specifies that how many numbers are remaining to be entered. Then find the largest and the smallest number.
Sample Execution:
Enter Number: 5
Enter Number: 10
Enter Number: 20
Enter Number: 12
Enter Number: 7
Enter Number: 6
The Largest number is 20
Smallest number is 6

Happy to help :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.