After a brief read I think you're running up against a common
misconception about how jQuery attaches events to objects.

When you do something like this:

  $('p').click( function() { alert('blah'); } )

All the _currently existing_ 'p' elements will have a click handler attached.

Now if you go on to add other 'p' elements to the page they _will not_
have the your click handler attached to them. You would need to
"rerun" the $('p').click( function() { alert('blah'); } ) on the new
elements to attach the handlers to them.

You might like to look at the "LiveQuery" plugin (
http://plugins.jquery.com/project/livequery ) as is manages all newly
added elements so they get the previously attached handlers attached
to them when they're added to a page.

Karl Rudd

On Jan 21, 2008 1:44 PM, Matt Quackenbush <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm working on a form that allows the user to click an "Add an Item" button
> in order to add another item to their order.  When this button is clicked, I
> have jQuery add another couple of fields to the form, so the user can input
> the data.  Along with the added form fields, a "Remove This Item" button is
> also added to the DOM.  All of this portion works nicely.  It's the reverse
> that I'm having trouble with.
>
> What I'm trying to do is to call empty() on the container of the clicked
> "Remove This Item" button (actually, an anchor).  I am getting no errors at
> all in FireBug, but when the anchor is clicked, the jQuery script is
> completely ignored and the browser follows the link.
>
> So, my question is: does jQuery support manipulation of an element that has
> been added to the DOM via Javascript?  I would assume that it does, but I'm
> not understanding why my "remove" function (code below) is being ignored.
>
> $("a.remove-item").click(function() {
>     var splt = $(this).attr("id").split("-");
>     var n = splt[1];
>     $("#container-" + n).empty();
>     return false;
> });
>
>
> Thanks in advance,
>
> Matt
>
>

Reply via email to