I'm building a program where you have an input box and an button, when user presses the button, the button value is displayed on the input box. I've tried the following: -
-
-
function calculation() {
-
value1=parseInt(document.getElementById("x").value); //input box
-
nine=parseInt(document.getElementById("nine").value); // button
-
-
value1=nine;
-
}
-
-
What I'm I doing wrong?
22 2879
Heya, Divina.
The problem you're having is that when you use parseInt(), you're no longer working with a reference to x's value; instead you're using a copy of it.
To make this work, you'll want to do something like this: -
function calculation() {
-
value1=document.getElementById("x").value; //input box
-
nine=parseInt(document.getElementById("nine").value); // button
-
-
value1=nine;
-
}
-
Note that since you never actually do anything with value1's value, there is no need to parseInt() it anyway.
You're looking for [code=javascript], by the way ~_^
Acoder, I've tried the what you said and still it didn't work, Can you see my coding below, (thank you for your quick response): -
<html>
-
<head>
-
<script>
-
function calculation() {
-
value1=document.getElementById("x").value;
-
nine1=parseInt(document.getElementById("nine").value);
-
-
value1=nine1;
-
}
-
</script>
-
-
</head>
-
<body>
-
-
<input type="text" id="x" size="25"/>
-
<input type="button" id="nine" value="9" onclick="calculation()">
-
-
</body>
-
</html>
Heya, Divina.
Hm. Ok, try this: -
function calculation() {
-
value1=document.getElementById("x"); //input box
-
nine=parseInt(document.getElementById("nine").value); // button
-
-
value1.value = nine;
-
}
-
Thank you Acoder that worked, thanks alot again.
Thank you Acoder that worked, thanks alot again.
Glad you got it working, but I should point out it was pbmods that helped you!
Final post of the day, In my Calculator Program I press the button 9, then I press button +, then I press 8, then I press button =, which will then display the result (17) in the text box.
Without using Objects, what is the best way to get this working. Please see code below: - <html>
-
<head>
-
<script>
-
-
function digit8() {
-
value1=document.getElementById("x");
-
Eight=parseInt(document.getElementById("eight").value);
-
-
value1.value=Eight;
-
}
-
-
function digit9() {
-
value1=document.getElementById("x");
-
Nine=parseInt(document.getElementById("nine").value);
-
-
value1.value=Nine;
-
}
-
-
function equals() {
-
value1=document.getElementById("x");
-
Nine=parseInt(document.getElementById("nine").value);
-
Eight=parseInt(document.getElementById("eight").value);
-
}
-
-
</script>
-
-
</head>
-
<body><br>
-
-
<input type="text" id="x" size="25"/>
-
<input type="button" id="eight" value=" 8 " onclick="digit8()">
-
<input type="button" id="add" value=" + " onclick="">
-
<input type="button" id="nine" value=" 9 " onclick="digit9()"><br><br>
-
-
<input type="button" id="equals" value=" = "onclick="">
-
-
</body>
-
</html>
Glad you got it working, but I should point out it was pbmods that helped you!
lol It's the eyes.
I don't understant the comment: "Change operand if User pressed 'c' or hasn't entered a number yet".
Ta
Heya, Divina.
I don't understant the comment: "Change operand if User pressed 'c' or hasn't entered a number yet".
Think about how a physical calculator works. Suppose you were to press '1', then '+'. Then suppose you changed your mind and pressed '-'.
The way the calculator would handle that would be to replace the '+' with a '-' in the expression.
Similarly, the goal here is to replace the current operand with the new one.
For example, suppose your calc array looked like this: -
calc = [
-
'12',
-
'+',
-
'15',
-
'*'
-
];
-
If the User presses the '-' button, you want to change calc to this: -
calc = [
-
'12',
-
'+',
-
'15',
-
'-' <--
-
];
-
When addToCalc('-') runs, it pops off the last element in calc. -
calc = [
-
'12',
-
'+',
-
'15'
-
];
-
-
last = '*';
-
Then, we enter the switch statement, and we hit the case '-' (line 22).
So we switch(last) (line 26). Since it is an operand (case '*', line 35), we ignore the value of last and instead push the '-' onto calc (line 38): -
calc = [
-
'12',
-
'+',
-
'15',
-
'-'
-
];
-
-
last = '*';
-
Then we break, go to the alert statement, and you should see this:
12 + 15 -
Sorry Sorry Sorry Pbmods for your hard work, I think it's a bit complex for me, I think there is an easier way but I've not sure how to implement it.
I was thinking user presses 8 + 9 =
Hence for each of these buttons I store it in a variable and then when user presses =, I do the sum when user presses =
Can you help me implement this?
Ta
Heya, Divina.
No problem. I had fun writing it anyway.
Sounds like what you'll want to do is create four textboxes: -
<input id="number1" value="" />
-
<input id="operand" value="" />
-
<input id="number2" value="" />
-
<input id="answer" value="" />
-
I presume you want the User to use number buttons instead of being able to type in the numbers; is this correct?
Similarly to what you were doing before, you'll want to store a reference to the text box that you want to edit: -
/***************
-
*
-
* Initialize textbox as a global variable; we start with the first number.
-
*/
-
var current = document.getElementById('number1');
-
-
<input type="button" id="eight" value=" 8 " onclick="digit(8);">
-
<input type="button" id="add" value=" + " onclick="digit('+');">
-
<input type="button" id="nine" value=" 9 " onclick="digit(9);"><br><br>
-
To implement the digit() function: -
function digit(value)
-
{
-
/***************
-
*
-
* Check to see if value is a number. If it is, append it to current.value.
-
*
-
* If it is an operand, set the value of the 'operand' textbox and then change current to reference the 'number2' textbox.
-
*/
-
}
-
y is a Global Variable to store operators (+,/,*,-), however i can't get the 'if' statement to work below: - function equals() {
-
value1=document.getElementById("v");
-
-
if (y=="+") { // I think the problem is here!!!!
-
alert("+ was selected");
-
var answer = (x+z);
-
value1.value=answer;
-
}
-
else if (y=="*") { // I think the problem is here too!!!!
-
alert("* was selected");
-
}
-
}
Heya, Divina.
[code=javascript] works better than [CODE : javascript] ~_^
Try alert()ing y to see if it has a value.
y is a Global Variable to store operators (+,/,*,-), however i can't get the 'if' statement to work below: - function equals() {
-
value1=document.getElementById("v");
-
-
if (y=="+") { // I think the problem is here!!!!
-
alert("+ was selected");
-
var answer = (x+z);
-
value1.value=answer;
-
}
-
else if (y=="*") { // I think the problem is here too!!!!
-
alert("* was selected");
-
}
-
}
Not an expert here but just to point this out in
"value1=document.getElementById("v");"
if your using that as your variable (which is y but i dont see the y variable any where) then your problem lies in your value1 because your variable Id is "v" not "y". most likely thats not your problem but just wanted to point it out if you are using it as a variable.
Thanks, Death Slaught
I have alerted 'y', the 'y' value is ok as I get '+' or '-' depending on the one the user selects.
It's just that the following line is not being excuted for some reason:
Also, 'y' is a global variable hence I have declared it at the top (yep I have not included this in the code). ID/Name 'v' is where the result of the calculation is going to be displayed.
Ta
Perhaps you need to display more of your code so we can see how you're calling equals().
Here is my entire code, see equals() section: - <html>
-
<head>
-
<script>
-
-
var x, y, z, value, answer;
-
-
function digit2() {
-
value1=document.getElementById("v");
-
Two=parseInt(document.getElementById("two").value);
-
value1.value=Two;
-
if (x==null) {
-
x=Two;
-
alert("in function 2: " + x);
-
}
-
}
-
-
function mul() {
-
value1=document.getElementById("v");
-
Mul=document.getElementById("mul").value;
-
y=Mul;
-
}
-
-
function sub() {
-
value1=document.getElementById("v");
-
Sub=document.getElementById("sub").value;
-
y=Sub;
-
}
-
-
-
function digit9() {
-
value1=document.getElementById("v");
-
Nine=parseInt(document.getElementById("nine").value);
-
value1.value=Nine;
-
if (x==null) {
-
x=Nine;
-
alert("in function 9: " + x);
-
}
-
}
-
-
function equals() {
-
value1=document.getElementById("v").value;
-
-
alert("X: " + x);
-
alert("Y: " + y);
-
alert("V: " + value1);
-
-
if (y=="*") {
-
answer= (x * value1);
-
alert(answer);
-
value1=document.getElementById("v").value=answer;
-
}
-
-
else if (y=="-") {
-
answer= (x - value1);
-
alert(answer);
-
value1=document.getElementById("v").value=answer;
-
}
-
-
}
-
</script>
-
-
</head>
[HTML]<body><br>
<input type="text" id="v" size="25"/>
<input type="button" id="two" value=" 2 " onclick="digit2()">
<input type="button" id="nine" value=" 9 " onclick="digit9()"><br><br>
<input type="button" id="mul" value=" * " onclick="mul()">
<input type="button" id="sub" value=" - " onclick="sub()"><br><br>
<input type="button" id="equals" value=" = "onclick="equals()">
</body>
</html>[/HTML]
Heya, Divina.
Couple of things that you might want to fix:
You never defined the value of the variable 'Two'. Perhaps you meant this instead:
Similarly for these lines: -
y=Mul;
-
.
-
.
-
.
-
y=Sub;
-
.
-
.
-
.
-
value1.value=Nine;
-
You'll want to make these: -
y = '*';
-
.
-
.
-
.
-
y = '-';
-
.
-
.
-
.
-
value1.value = 9;
-
Thank you very very much, you are very good at your job and you pointed out some obvious stuff, it's so much easier the way you have suggusted.
once again cheers
Divina XXX
One more thing, with my current code I can only input one digit e.g. 1, I can't into two or more digits e.g. 564, 487, 9999 etc.
how can i resolve this?
Is it due to:
Ta
Divina
Heya, Divina.
You got it.
The easy way to do this is to use string concatenation, i.e., the plus operator ('+').
Now the tricky part is that you are working with numbers here, and the plus operator is also overloaded for addition.
In other words: -
7 + 8 == 15
-
-
// But...
-
'7' + 8 == '78';
-
I think you can see where this is going. Even though we're working with numbers, we have to convert them to strings until it is time to do the actual calculations!
So going back to this line:
What we need to do here is cast value1.value as a String and then concatenate the 8 onto the end of it. -
value1.value = String(value1.value) + '8';
-
Note that in your equals() function, you're going to also need to make this change: -
value1 = Number(document.getElementById("v").value);
-
to cast the string back into a number. Otherwise, you might end up concatenating when you meant to do real addition!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JCO |
last post by:
How's come when I press the enter key, I can't get it to execute the correct
password.
It seems that I'm forced to press the button. I want to be able to do both.
How is this done?
|
by: Don Grover |
last post by:
I have a table thats wrapped in a div tag, that when user selects 1 of 2
radio buttons it hides or shows table, this works ok.
But I want to set the table show hide on what the existing state of...
|
by: jforena |
last post by:
Hi,
I am trying to create a javascript that will display a confirmation
message when a user submits a form and has specifically selected
a particular radio button.
For example:
<form...
|
by: Codeman II |
last post by:
Hi there,
I am building a form where the user must upload a picture and fill in his
details.
Now I have a problem as all of this is on the same form.
How will I be able to have the Browse...
|
by: hust6 |
last post by:
I created a form using html, but am having trouble with one issue.
If I user clicks "no" on one of my radio buttons, I want a drop down
selection to appear next to it to display several "reasons"...
|
by: IchBin |
last post by:
I can not see what the problem is with this script. I am just trying to
set a radio button by calling setCheckedValue('abbr_letter', 'V'). Sorry
I am new to javascript.
<html>
<head>
<script...
|
by: almurph |
last post by:
Hi everyone,
I'm a newbie to javascript. I have written some code to detect when a
user presses the down-arrow, up-arrow and enter button. Essentially
the user can arrow down or up through a...
|
by: sparksol |
last post by:
I have a form with a drop down box. If you select an option in the drop down box (depending which option is selected) one or two textbox(es) and a submit button display.
I would like to keep the...
|
by: daonho |
last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |