Professional Documents
Culture Documents
Counting Clicks PHP JQuery
Counting Clicks PHP JQuery
Counting Clicks PHP JQuery
Whynotdosomethinglikethis?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
<inputtype="button"value="Clickme"id="clickme">
<scriptsrc="https://code.jquery.com/jquery2.1.1.min.js"></script>
<script>
$(document).ready(function()
{
varelement='<inputtype="text"id="textbox">';
varcount=0;
$("#clickme").on("click",function(event)
{
event.preventDefault();
count++;
$(element).insertAfter('#clickme');
});
});
</script>
Ifyouneedexplanationonwhatiswrongwithyourcode:
youdidahideofyourelementonline7
you'veputanonClickeventbutthatwasequaltoasubmit...whichIthinkresultsinapagerefresh
you'veclonedyourelement(hiddenelement),butyoudidn'tappendittoyourhtml.
Regardingmycode,youdon'tactuallyhavetocountthetimesabuttonispressedsoyoucan
eliminatecount=0;andcount++;lines(unlessyoureallyneedthem).
0
AleMonteiro2381YearAgo
Thefirsttimethebuttonisclickedyou'regoingtocloneitonce?
Thesecondtimethebuttonisclickedyou'regoingtoshowittwice?Andsoon?
Then,itsouldbesomethinglike:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
$(function()
{
varelement=$("#textbox").hide();
varcount=0;
$("#clickme").on("click",function()
{
count++;
for(vari=0;i<count;i++){
element.clone().insertAfter("#clickme");
}
});
12. });
Thisway,atthethirdclick,there'llbe6elements(1forthefirstclick,2forthesecond,3forthethird).
Here'saworkingscriptforyoutoplayaroundwith:
<!DOCTYPEHTML>
<html>
<head>
<metahttpequiv="ContentType"content="text/html;charset=utf8">
<title>Incrementcountwhenbuttonisclicked</title>
</head>
<body>
<inputtype="button"value="Count"id="countButton"/>
<p>Thebuttonwaspressed<spanid="displayCount">0</span>times.</p>
<scripttype="text/javascript">
varcount=0;
varbutton=document.getElementById("countButton");
vardisplay=document.getElementById("displayCount");
button.onclick=function(){
count++;
display.innerHTML=count;
}
</script>
</body>
</html>
Hopefullyitshouldbeclearenoughwhat'sgoingon,butifyouhaveanyquestions,don'thesitatetoask.