Welcome Guest, Not a member yet? Register   Sign In
unique numbers for jquery
#1

(This post was last modified: 12-29-2016, 03:51 AM by wolfgang1983.)

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;
      }
});
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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




Theme © iAndrew 2016 - Forum software by © MyBB