tags:

views:

979

answers:

6

Hi guys.

I'm not able to bind events to a list that is generated by jQuery. I've looked at some similar issues, but havent found any solution.

This is the code that generates my list:

var list = '<ul>';
for (var i = 0; i < data.length; i++) 
  {
  list += '<li id="' + data[i].id + '"><span class="btnRemoveItem" title="Remove item from list"></span>' + data[i].name + '</li>';
  }
  list += '</ul>';
  jQuery("#spanContainer").html(list);

This creates my wanted list. Works great. But I'm not able to manipulate the list trough jQuery. Some research points me in the direction of binding events to my list. How do I do that? I've looked at several posts having the same issue, but haven't found anything that solved my problem.

I'm using jQuery 1.2.6 and cannot use v.1.3 (which has the .live function).

Resources: docs.jquery.com/Events/bind

Edit: I've tried this, but it's not working.

jQuery("#7276").bind("click", function() { alert('test'); })

This the the html output:

<span id="itemList">
  <ul>
    <li id="listItem_7276"><span title="Remove item from list" class="btnRemoveItem" id="7276"/>Main item</li>
    <li id="listItem_7281"><span title="Remove item from list" class="btnRemoveItem" id="7281"/>Test item 4</li>
  </ul>
</span>
A: 

The livequery plugin works with jQuery 1.2.6 and does essentially the same as the live() event (and more)

Philippe Leybaert
A: 

You must append the elements to the DOM before you can bind to them.

See docs on append() and appendTo() jquery calls.

yhager
I think that's what he's doing by calling html(list). It replaces the HTML, so it will add the items to the DOM
Philippe Leybaert
For some reason, I thought html() does not make the elements bind()able.. I have no time to check now, so I'll take your word..
yhager
A: 

I'm not sure what do you mean by manipulating list via jquery, but if you want bind some events handlers to elements of your list you can use:

$("ul span.btnRemoveItem").click( yourOnClickHandler);

this will add the onClick handler for all span elements in your list. Or you can access whatever you using:

$("ul span.btnRemoveItem").bind( "event", yourOnEventHandler);

And if you want to access each list element separately after you have inserted it into DOM, you can do:

$( "ul li#" + data[i].id).dosomething(); // here you can specify whatever you want to do.
Artem Barger
+5  A: 

Try building the data structure in jquery: (untested code)

ul = $("<ul/>");
for (var i = 0; i < data.length; i++)  {
ul.append(
 $("<li/>")
 .attr("id", data[i].id)
 .append($("<span/>")
  .addClass("btnRemoveItem")
  .attr("title", "Remove item from list")
  .click(function() {
   $(this).parent().remove();
  })
 ).append(data[i].name)
);
}
jQuery("#spanContainer").html(ul);
Paul Tarjan
+1 I know that works as I use it myself.
karim79
+1 - No need to use the livequery plugin
Russ Cam
Ok, I replace my code with this one. But no ouput. Using "console.log(ul)", the output is [object object]. And why only apply the '/'end tag? jQuery applies the first tag?
Steven
Sorry it was just a snipped. I'll fill it in.
Paul Tarjan
Aha! Excellent! I thought that I didn't need the .html(ul) with the above code. Now it outputs everything. And I'm a whole lot wiser on building html structure using jQuery. Thanks alot!
Steven
Note: the .click is missing a ')' at the end. Thought I should nemtion that for other cut'n'pasters :)
Steven
Fixed. Thanks Steven.
Paul Tarjan
A: 

Hmm...that html function should work when appending your list to the #spanContainer.

Since you can't use live(), you'll have to manually re-bind the events after appending the list to #spanContainer like this:

function listGenerator(){
  ...//create the list
  jQuery('#sc').html(list);
  jQuery('#sc ul li').bind('click', function(){...});
}

(Also, the jQuery each() function could help you in generating that list...you may want to look into it)

btelles
I've tested jQuery("#7276").bind("click", function() { alert('test'); }) - where #7276 is he ID of the span "button". But nothing happend.
Steven
Hmmm...when are you binding the event? When the page loads? You may have to rebind them per my edits above.
btelles
A: 

Hi Steven,

I can't see the code where you assign the events to your list. You'd still want something like this:

$("#spanContainer span.btnRemoveItem").click(function () { 
  $(this). ... ;
});

Alternatively - if you're specifically asking about the live functionality new to 1.3, then you can use a UI plugin for an older version of jQuery called livequery.

http://docs.jquery.com/Plugins/livequery

So something like this:

$("#spanContainer span.btnRemoveItem").livequery('click', function(e){
  $(this). ...;
});

Joerg

Joerg