views:

31

answers:

2

Hi ,

I am using jquery 1.3 ,

When i clone the element with clone(true) all the data + event are copied , but the problem is to distinguish between event of the original html block and cloned html block.

When i tries to fire event to cloned html blocks then original html block's event also get fired.

so now the big problem is how to distinguish between events

checkout the code over here jsfiddle.net/BbBqJ/1

A: 

Use the event target and it should only interact with the origin of the event...

$("something").bind("click", function (event) {
   $(event.target).css("border", "1px solid #f00");
});
Andir
A: 

When you have a selector like $('.edit') for example, you are selecting all of that element on the page.

To focus the selector to a limited context, you can pass a second argument. So what I did was to store the new element you created into a variable called $box, and then passed that in as the context like $('.edit', $box).

I made other changes to your code to clean things up too.

http://jsfiddle.net/sxkxp/1/

Again, just remember that a selector like .edit is affecting all those elements that exist on the page.

patrick dw
thanks for the help , but if i assign a different class name for the clone block to keep track of blocks and if i click on addmore then i can see that the context of events are belongs to the original block only http://jsfiddle.net/sxkxp/7/
Hunt
@Hunt - Actually, you can see that the context is correct, since the new `<input>` shows up in the correct place. The reason you're seeing the old class name pop up is that you're doing `alert($newBox.attr('class'))` where `$newBox` still refers to the variable that was used to create the box. That variable was cloned with the rest of the handler. But you don't need it anymore. Try placing this alert on the next line down `alert($box.attr('class'))` and you'll see that you're in the right one. Please note my additional comments in the example as well: http://jsfiddle.net/sxkxp/9/
patrick dw
Thanks a lot patrick for helping out , can you just tell me how one can polish the knowledge of jquery as after working so much of time in it i just now came to know that one can pass second argument as a context by you :)
Hunt
@Hunt - You're welcome. :o) So many things to know. So much of what I've learned has come from reading the jQuery questions and answers here on SO as they come in. FYI - Doing `$('.someClass', $someContext)` is exactly the same as doing `$someContext.find('.someClass')`. jQuery converts it to use `.find()` in the background. http://github.com/jquery/jquery/blob/1.4.2/src/core.js#L146 :o)
patrick dw