0

I am trying to assign ids to my div on the page the div's are not created dynamically but are once written on the page and i need to assign random id's to those div that exist on my page. tried many thing but was unable to get solution.. And on the html page the div exists in the class called demo and to the div's i have assigned id=mydiv. And later the script is called and divs id is changed to random number from mydiv.. i am unable to add +1 to the next div. Tried jquery traversing as shown below nextall() but it doesnt add the +1 to the new div..

function handledivIds() {
    var e = $(".demo #mydiv");
    var t = randomNumber();
    var n = "div-",t;
    e.attr("id", n);    
    e.nextall().attr("id", n+1)
    }

function randomNumber() {
    return '' + new Date().getTime();
}
2
  • And guys how unique ll that random number() generate or any changes Commented Jun 29, 2014 at 4:15
  • Thx a lot guys everybodys ans workd great Commented Jun 29, 2014 at 4:18

4 Answers 4

2

Try changing this

var e = $(".demo #mydiv");

to this

var e = $(".demo div");

ID only works for one element so #myDiv is only going to return one of the div elements...ever.

Also incorporate Jai's answer.

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

Comments

1

Demo

  • n+1 wont work for string, all the next divs will get same Ids with .nextAll().
  • Also concatenate using + - var n = "div-"+t;
  • Use .each() to iterate over div elements.

Try this,

function handledivIds() {
    var e = $(".demo div");
    var t = randomNumber();

    e.each(function (index) {
       $(this).attr("id", "div-" + (t + index))
    });
}

function randomNumber() {
    return '' + new Date().getTime();
}

1 Comment

@user3787062, Jai was the first to answer and point the mistake. You should accept his answer. The problem was mainly in concatenation.
0

change this:

var n = "div-",t;

to this:

var n = "div-";

May be you want to concatenate the string here and in your nextAll() you are appending 1 to that string.


Or you can update your function to this:

function handledivIds() {
    $(".demo div").each(function(i, it){
        $(this).attr('id', "div-"+$(this).index()); // assigns unique ids per div
    });
}

2 Comments

and how do i assign the first one ;
then var n = "div-"; change this.
0

First - using the date is not random. It will provide predictably increasing values. If you need something truly random, you'd be better off assigning the DIV ids server-side with an actual random number generator. If you can live with pseudorandom numbers, use Math.random instead of the time. To guarantee uniqueness you can use both the time and a random number. The inclusion of the time, since it's monotonically increasing, will guarantee unique values, while the pseudorandom number will give it the random quality. If you need truly random, use a cryptographically strong random number generator server-side.

Second, your original divs cannot all have the same starting id. Better to give them a class server-side then use that class to identify them.

 <div class="needs-random-id"></div>

Lastly, nextAll applies the same attribute to all the matching elements. You probably want to use each

  var max = 9007199254740990;
  var min = 1;
  $('div.needs-random-id').each(function() {
      var time = new Date().getTime();
      var idValue = 'div-' 
                       + time + '-' 
                       + Math.floor(Math.random() * max) + min;
      $(this).attr('id', idValue);
  });

1 Comment

@user3787062 - it would take some work to make sure they are unique - they're random so you can't guarantee. One potential solution would be to concatenate the current time with a random number, making a compound id that would be unique. I'll update. Again, though, they are only pseudorandom, not truly random. If you need truly random do it serverside with a cryptographically strong random number generator.

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.