CodeIgniter Forums

Full Version: unique numbers for jquery
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am creating a hyperlink insert for my codeigniter form that I have

My script creates links like where has a unique number for each link


PHP Code:
[example][1]

[
1]: http://www.example.com/ 



My Question is if there like below where number 1 & 3 show if I create a new hyper link it should be able to set the next available number which is 2 and so on.

Live demo



PHP Code:
[example][1]

[
1]: http://www.example.com/


[example][3]

[
3]: http://www.example.com/ 



Code:
$('#myLink').on('shown.bs.modal', function() {
    var text = getSelectedText();
    $('#title').val(text.trim());
    $('#url').val('http://');
});    

function getSelectedText() {
    var textarea = document.getElementById("message");
      var len = textarea.value.length;
      var start = textarea.selectionStart;
      var end = textarea.selectionEnd;
      var sel = textarea.value.substring(start, end);
    return sel;
}

var counter = 1;

$('#save-link').on('click', function(e) {
    var textarea = document.getElementById("message");
      var len = textarea.value.length;
      var start = textarea.selectionStart;
      var end = textarea.selectionEnd;
      var sel = textarea.value.substring(start, end);
      
      var replace = '[' + $('input#title').val() + ']' + '[' + counter + ']';

      var id = '\n   [' + counter + ']: ' + $('input#url').val();

      counter++;
      
      if ($('#title').val().length > 0) {
          textarea.value = textarea.value.substring(0,start) + replace +
          textarea.value.substring(end,len) + '\n' + id;
          $('#myLink').modal('hide');
          $('#myLink form')[0].reset();
      } else {

          return false;
      }
});
you don't need javascript for this, simply make an auto-increment field, or use https://api.jquery.com/jQuery.uniqueSort/