1

I am needing to generate a reference number using jQuery (I understand not the best method, but best I have to work with).

Each existing number is inserted as a class, these are hidden on the page, just available for this use:

<div class="Ref 14001"></div>
<div class="Ref 14002"></div>
<div class="Ref 14003"></div>

The Reference number is sequential, and the "14" prefix denotes the year. At the start of each year the number needs to reset as "YY001". So at the start of 2015 I need it to automatically change to:

<div class="Ref 15001"></div>

So what needs to happen is when the page is loaded a script runs, finds the most recent number, in this case "14003", I need to then check the first two numbers are the current year, if not, update and reset the number. If they are, just add "1" (14004) and insert into a field:

<input type="text" class="ref-number" name="ref-number" />

My jQuery knowledge is very basic, so code samples or jFiddles would be greatly appreciated. Thanks for any assistance in advance.

EDIT: Thanks for directing me to those posting guidelines, I will endeavour to post my examples. I have a jsFiddle for the first part of my question. I can find the largest number (by class then ID from suggestion) and increase by 1: http://jsfiddle.net/PKMYy/

It's the other part of my problem that I don't even know where to start.

  • Why not use php or other programming language? – Iago Jun 18 '14 at 01:41
  • 2
    Hi there. Stackoverflow is designed to help people like you find solutions to their problems, however an attempt to solve the problem yourself is required. We are not here to write your code/solution for you but rather to help you fix problems with your attempts. Please see [How to Ask](http://stackoverflow.com/questions/how-to-ask) – scrowler Jun 18 '14 at 01:41
  • +1 @scrowler. Please show us what you've tried and how far that took you and what did not work as expected. – PeterKA Jun 18 '14 at 01:47

1 Answers1

1

Since your reference isn't always the same, it sounds more like an id rather than a class.

You could split it up like this:

<div id="14001" class="ref"></div>
<div id="14002" class="ref"></div>
<div id="14003" class="ref"></div>

You can find all of these divs with:

$('.ref')

...and then iterate through them, finding the highest number, and adding 1.

If your divs are always sequential, meaning the last number also is always the highest number, then I think you can use last() to get at the last element in the list, pull the id from that div, and add one.

Something like:

var nextId = parseInt($('.ref').last().id) + 1;

To handle the parsing of the two-digit year, if the ids are always 5-digits long, then you can divide it by 1000, which would be (rough pseudocode):

yearOfLastId = 14003 / 1000 = 14
yearOfToday = new Date().getYear() % 100  // (2014-06-18 .getYear() = 114 % 100 = 14)
if yearOfLastId != yearOfToday
  nextId = yearOfToday + '001';
else
  nextId = yearOfLastId + '' + nextId;
jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Beware: [HTML < 5 won't like having IDs that start with a number](http://stackoverflow.com/a/79022/2812842) - best to use data attributes instead? – scrowler Jun 18 '14 at 01:58