Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How could i increase button click date by +1 on click

Right not in my button click, i have this code

JavaScript
 function addNewDateFrom(){

    var numberToAddafter = 0;
    var toTime = new date();
    toTime.setDate(toTime.getDate() + numberToAddafter++);
}

var textNodeAddFreeTime = document.createTextNode("+");
var buttonCreateAddFreeTime = document.createElement("button");

buttonCreateAddFreeTime.appendChild(textNodeAddFreeTime);
 buttonCreateAddFreeTime.onclick = addNewDateFrom;



The first time i click the button gets 1
But second time still 1?

Why?
Posted
Updated 10-Dec-14 4:00am
v3
Comments
DamithSL 10-Dec-14 9:39am    
update the question with full code, where you define totime and numberToAddafter?
which event you increase by one day?
Kurac1 10-Dec-14 9:40am    
I have in my code toTime and numberToAddafter?

try
JavaScript
var toTime = new Date();

function addNewDateFrom(){
    toTime.setDate(toTime.getDate()+1);
}


DEMO[^]
 
Share this answer
 
v3
Comments
Kurac1 10-Dec-14 9:52am    
Calender is not supported?
DamithSL 10-Dec-14 10:00am    
you tagged as java, that's why all these misunderstandings, please update the tags in your question first, next time remember to tag the question correctly
Kurac1 10-Dec-14 10:00am    
That will only increase one day and not on every onclick.
DamithSL 10-Dec-14 10:06am    
check my updated answer
Kurac1 10-Dec-14 10:09am    
It only adds one when i am doing it.
Is this really Java or javascript? It looks like the latter to me.
JavaScript
function addNewDateFrom(){
    var numberToAddafter = 0;  // set number to add as 0
    var toTime = new date();   // get a new time
    toTime.setDate(toTime.getDate() + numberToAddafter++);  // add zero to the time
}

You start each time with a new (zero) value in numberToAddafter, and since this is a local variable, incrementing it to 1 after you add it to the time, serves no purpose, as the variable is immediately deleted.
 
Share this answer
 
Comments
Kurac1 10-Dec-14 9:54am    
It javascript, so how should i do? You did just post same code as me ?
Richard MacCutchan 10-Dec-14 9:58am    
Yes, and I added comments and explanation as to why it does not work.
Kurac1 10-Dec-14 9:59am    
I already know that u posted, but don't now how to do it else.
Richard MacCutchan 10-Dec-14 10:10am    
You need to create your date variable outside of this function and just add 1 to it every time the button is clicked.
Hi,
try this
C#
var toTime = new Date();
Var counter=0
function addNewDateFrom(){
    toTime.setDate(toTime.getDate()+counter);
counter=counter+1;
 }
-
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900