Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Div Create dynamic and click

Div Create dynamic and click

Scheduled Pinned Locked Moved JavaScript
javascriptarchitecture
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anishkannan
    wrote on last edited by
    #1

    Legend create dynamic , I used jquery for create Legend GUI with click callback. each div have ID. expectation is while clicks div, the corresponding ID want to show. But here its showing only at last created div id only

    for (var i = 0; i < 3; i++) {
    var id = "test" + i;
    $("

    ", {
    "id": id,
    "style": "width:30px; height:30px cursor:pointer; border:1px solid Green ;padding:1px; background-color:Green",
    click: function() {
    alert("ID = "+ id);
    }
    }).appendTo("#legend");

    M 1 Reply Last reply
    0
    • A anishkannan

      Legend create dynamic , I used jquery for create Legend GUI with click callback. each div have ID. expectation is while clicks div, the corresponding ID want to show. But here its showing only at last created div id only

      for (var i = 0; i < 3; i++) {
      var id = "test" + i;
      $("

      ", {
      "id": id,
      "style": "width:30px; height:30px cursor:pointer; border:1px solid Green ;padding:1px; background-color:Green",
      click: function() {
      alert("ID = "+ id);
      }
      }).appendTo("#legend");

      M Offline
      M Offline
      Manfred Rudolf Bihy
      wrote on last edited by
      #2

      The problem is the function you created doesn't capture the value of the variable id, but the reference to that variable via a closure. To get a function that captures the current value of id create a function called createFunc(id) like this:

      function createFunc(id)
      {
      var tmp = id;
      return function() { alert("ID = " + tmp); };
      }

      then use it in your code like this:

          ...
          click: createFunc(id);
          ...
      

      Cheers!

      —MRB

      modified on Wednesday, July 6, 2011 12:15 PM

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups