views:

134

answers:

3

hello, i have this code

$(document).ready(function() {
     $('a.menuitem').click(function() {
   var arr = 0;
   var link = $( this ), url = link.attr( "href" );
   var newDiv = $( document.createElement( 'div' ) )
   $( "#content_pane" ).append( newDiv );
   newDiv.load( url );
   return false;
  });
 });

As you can see i am creating a div and adding some content to it, how would i give each dic that is created a unique id, something section1 section2 section3 etc?

+6  A: 

Just use a counter:

var section = 1;
$(function() {
  $("a.menuitem").click(function() {
    ...
    $("<div></div>").attr("id", "section" + section++).appendTo("#content_pane");
    ...
    return false;
  });
});

Also, I'd suggest creating the element as per above.

cletus
A: 

If a counter, as cletus has shown, is not sufficient, then there is a jQuery UUID plugin available (but this is probably overkill).

Justin Johnson
A: 

You shouldn't need to do this. If you can store the id in a variable, you could store the element itself (well, a reference to the element) in a variable too.

nickf
It is not practiacal to store the reference to the div object in some situations.
BYK
such as? my point is that you're going to be storing and passing around a string, just so you can get a reference to an element, you might as well pass the element itself.
nickf
How would I create multiple DIV's onclick in multiple variables? I need this beuase it is the only way, I could think of removing the element from the page onclick of a link
sico87
multiple variables? https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array
nickf