Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HTML
<input type="button" class="btn btn-primary btn-sm" id="testbtn" value="LIKE">
<script>
        var buttonclicked;
    $("#testbtn").click(function(){
    if( buttonclicked!= true) {
        buttonclicked= true;
        if (post.post_variable == "NULL")
            post.post_variable = 0;
        post.post_variable = post.post_variable + 1;
        alert("Button is clicked for first time");
    }else{
        alert("Button isn't clicked for first time");
    }
    });
  </script>

this the html and js code for that and i want to add 1 in post.post_variable variable which is a variable in my database model.

Python
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable = False)
    post_variable = db.Column(db.Integer, default = "NULL")

   

plz help me out i am stuck in this.

What I have tried:

I have tried the above code but its not working.
Posted
Updated 7-Jul-20 21:14pm

You never increment the counter if buttonclicked is true. Try modifying the code thus:
JavaScript
if( buttonclicked!= true) {
    buttonclicked= true;
    if (post.post_variable == "NULL")
        post.post_variable = 0;
    alert("Button is clicked for first time");
}else{
    alert("Button isn't clicked for first time");
}
post.post_variable = post.post_variable + 1; // always increment the counter
 
Share this answer
 
You can try the followin -

HTML
HTML
<pre><input type="button" class="btn btn-primary btn-sm" id="testbtn" value="LIKE">

<div id="output">0</div>


Java
Java
$(function() {
  $('#testbtn').click(function() {
    $('#output').html(function(i, val) {
      return val * 1 + 1;
    });
  });
});


Just add your other required code or if blocks as required.
 
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