<!DOCTYPE html>
<html>
<body>
Please enter a number:<BR>
<input type="text" id="amount_input">
<button onclick="other_amount_input()">Submit</button>
<script>
var balance=502.73;
function other_amount_input() {
var input = document.getElementById("amount_input").value;
var new_total = parseInt(input) + balance;
alert("The amount deposited is: $" + input + ".00\n" + "This is the old balance: $" + balance + "\n" + "The new total is : $" + new_total);
}
</script>
</body>
</html>
I am trying to make an input box with html and javascript that takes an input (a positive number, can be a decimal but only to the hundredth place) I have the web page working to where it will take a whole number and add it to a hard coded number and display the output which is "hard_coded_number + input_number = output_number"
all I had to do was change parseInt to parseFloat and it worked for me
I am just beginning with JavaScript and what I am trying to achieve now is to add bold style to a variable result. The code that I am refering to is the following:
if(isNumeric(n)) {
document.write("The square root of " + n + " is " + answer);
}
else {
alert('This is not a number!');
}
I want to make the variable answer to appear in bold and the result to look like this:
Example: The square root of 4 is 2
Thank you all in advance!
It's just a matter of adding some HTML ?
if(isNumeric(n)) {
document.write("The square root of " + n + " is <strong>" + answer + "</strong>");
} else {
alert('This is not a number!');
}
This is fine for testing, but in production you wouldn't really use document.write, and preferably you'd use an external stylesheet and a wrapper element to make parts of the text bold.
I have the following JSFiddle: http://jsfiddle.net/uX8ZQ/
Basically I am trying to achieve a continuing for loop until a variable gets to a certain number, loading 5 at a time. This is what I have so far:
HTML:
<button onclick="go()">Load</button>
<p id="demo"></p>
JavaScript:
var max=16;
var y=5;
function go(){
var x="";
for (var i=1;i<=y;i++){
if(i>max){
return;
}
x=x + "The count is " + i + "<br>";
}
document.getElementById("demo").innerHTML=x;
y=y+5;
}
The result I get is the loop stops at 15 and won't load 16
I am new to JavaScript and trying to learn my way through loops but this one I cannot seem to get.
By the way, in the JSFiddle I have used window.go = function(){ as JSFiddle will not work by simply defining the function. I am using the above code in my document.
Use break instead of return as return will exit the function before it gets to update the HTML.
function go(){
var x="";
for (var i=1;i<=y;i++){
if(i>max){
break;
}
x=x + "The count is " + i + "<br>";
}
document.getElementById("demo").innerHTML=x;
y=y+5;
}
I have a page where I want show multiple countdowns. I made a javascript code that makes the countdown and it works on all divs that i specify, but this goes in real time only on the last div. Can someone help me? I posted the page below.
<html>
<head>
<title>Timer</title>
<head>
<script type="text/javascript">
function Timer(){
this.countdown=function(fineanno, finemese, finegiorno, fineore, fineminuti, finesecondi, nomediv)
{
var_div=nomediv;
var_anno=fineanno;
var_mese=finemese;
var_giorno=finegiorno;
var_ore=fineore;
var_minuti=fineminuti;
var_secondi=finesecondi;
data_scandeza= new Date(var_anno,var_mese-1,var_giorno,var_ore,var_minuti,var_secondi);
data_oggi= new Date();
differenza=(data_scandeza-data_oggi);
giorni=parseInt(differenza/86400000);
differenza=differenza-(giorni*86400000);
ore=parseInt(differenza/3600000);
differenza=differenza-(ore*3600000);
minuti=parseInt(differenza/60000);
differenza=differenza-(minuti*60000);
secondi=parseInt(differenza/1000);
differenza=differenza-(secondi*1000);
if (giorni <= "0" && ore <= "0" && minuti <= "0" && secondi <= "0")
{
document.getElementById(nomediv).innerHTML="Tempo scaduto";
}
else
{
document.getElementById(nomediv).innerHTML=giorni +' giorni '+ore+' ore '+minuti+' min '+secondi+' sec';
setTimeout("t"+var_div+".countdown(var_anno, var_mese, var_giorno, var_ore, var_minuti, var_secondi, var_div)",1000);
}
}
}
</script>
</head>
<body>
<div id="div2"></div>
<script>
var tdiv2 = new Timer();
tdiv2.countdown("2013","04","26", "23","00","00","div2");
</script>
<div id="div3"></div>
<script>
var tdiv3 = new Timer();
tdiv3.countdown("2013","04","26", "23","00","00","div3");
</script>
</body>
</html>
var_div, var_anno, var_mese, var_giorno, var_ore, var_minuti and var_secondi are global variables, because you declared them without using the var keyword. That means all your calls to countdown will access the same variable.
Don't use the name of the variable in your setTimeout, or you'll get the current value of this global variable (the second one set). Instead, use the value of the variable at the time you build the settimeout, like this:
setTimeout("t"+var_div+".countdown(" + var_anno + ", " + var_mese + ", " + var_giorno + ", " + var_ore + ", " + var_minuti + ", " + var_secondi + ",'" + var_div + "')",1000);
In fact I don't see any need for this part at all:
var_div=nomediv;
var_anno=fineanno;
var_mese=finemese;
var_giorno=finegiorno;
var_ore=fineore;
var_minuti=fineminuti;
var_secondi=finesecondi;
Instead of using those new variables, you can just use the parameters that were passed in. I suppose the reason you had that is so the setTimeout would have a variable to read the value from, but with my suggested change above it is no longer necessary.
this is my coding. in this coding i want to print table in innerHTML in paragraph tag.This is working but the problem is when i click submit button this result show only last value like this "10*10=100" but i want to run full loop which i define in code from 1 till 10. Please solve this issue.
<html>
<head>
<title>Table Program</title>
</head>
<body>
<input type="text" id="table" placeholder="Enter table"/>
<input type="button" value="Submit" onClick="table()"/>
<p id="demo"></p>
<!----------------------------------------------------------------------------------------------->
<script type="text/javascript">
function table()
{
var value = document.getElementById("table").value;
var demop = document.getElementById("demo");
var a;
for(a=1; a <= 10;++a)
{
demop.innerHTML=(value+"*"+ a +"="+ value*a);
}
}
</script>
</body>
</html>
Your for loop is overwriting the innerHTML of demop each time it is executing.
So when your for loop reaches last iteration a will be 10 and hence you only get the last value "10*10=100"
So you should append the result every time you iterate your for loop
just make it like this
demop.innerHTML += (value + "*" + a + "=" + (value*a) + "<br />");
So you will get your output on seperate lines.
When you call demop.innerHTML = something you overwrite it each time.
You should either:
Put the entire string in a temporary result variable and then give it to innerHTML
Concatenate the different results by doing
demop.innerHTML = demop.innerHTML + (value+"*"+ a +"="+ value*a);