0

I need to increment a few different numbers in the same HTML ID every 24 hours. So if one number is at 1 and the other is at 20 regardless of what the number it is must be incremented by 1.

As you can see I just need a way to increment any number in the HTML ID by +1 instead of changing the number to 1. Here is my code

<p id="pColor">
    Flower are on day <span>(<span id="datePlus">1</span>)</span>
</p>
<p id="pColor">
    Fruiting Plants are on day <span>(<span id="datePlus">20</span>)</span>
</p> 

JS

function theDate() {
    var initialDate = new Date(2017, 0, 19); // Dec 1st 2012
    var now = Date.now();
    var difference = now - initialDate;
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    var daysSince = Math.floor(difference / millisecondsPerDay);
    console.log(daysSince);
    function dateUpdate() {
        if(daysSince >=1) {
            console.log("True");

            // THIS IS WHERE I WANT CODE TO GO

        } else {
            console.log("false");
        }
    }
}

theDate();
4
  • 3
    Identifiers must be unique You are using id="pColor" multiple times Commented Jan 20, 2017 at 6:51
  • This will help: stackoverflow.com/questions/9186346/… Commented Jan 20, 2017 at 6:53
  • @Satpal said correct <span id="datePlus">20</span> first make datePlus unique. Either use this as a class name. Commented Jan 20, 2017 at 6:53
  • Not sure what exactly you want to increment but you casn do so by just using ++. Example: var number = 0; number++; Commented Jan 20, 2017 at 6:54

4 Answers 4

1

Firstly you need class, Not id.

In a html page id can't be duplicate.

And increase the value of every elements using class.

// The .each() method is unnecessary here:
$( ".datePlus" ).each(function() {
  $( this ).html( parseInt($( this ).html()) + 1);
});

When this code will run, it will increase value by one in every element.

Sign up to request clarification or add additional context in comments.

Comments

0

You can add +1 with this for loop of jquery

for(var i = 1;i<7;i++)
{
  $("#demo").append(i+"</br>");//just for testing
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p id="demo"></p>

Comments

0

First, change id="datePlus" to class="datePlus".

Then use this function:

function theDate() {
    var initialDate = new Date(2017, 0, 19);
    var now = Date.now();
    var difference = now - initialDate;
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    var daysSince = Math.floor(difference / millisecondsPerDay);

    if(daysSince > 0) {
        $(".datePlus").each(function(){
            var $this = $(this);
            var number = parseInt($this.text());
            number = isNaN(number)? daysSince: number + daysSince;
            $this.text(number);
        });
    }
}

1 Comment

Thanks, the .each param would have made sense. Very helpful.
0

First, change the id pColor at one of your

elements, as ID must be unique identifiers. for example:

<p id="datePlus">Flower are on day <span >(<span id="datePlus">1</span>)     </span></p>
  <p id="datePlus">Fruiting Plants are on day <span >(<span id="datePlus2">20</span>)</span></p> 

then, you can take the specified elements:

 var number1 = $( "span#datePlus" ).text();
 number1 = parseInt(number1);
 var number2 = $("span#datePlus2").text();
 number2 = parseInt(number2);
 number1++;
 number2++;
 $("span#datePlus").text(number1);
 $("span#datePlus2").text(number2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.