Bug Tracker

Modify

Ticket #4604 (closed bug: duplicate)

Opened 4 years ago

Last modified 3 years ago

replaceWith is not removing old content before adding new content

Reported by: soichih Owned by:
Priority: minor Milestone: 1.4
Component: core Version: 1.3.2
Keywords: replaceWith Cc:
Blocking: Blocked by:

Description

I have multiple select boxes under a div, and when I use replaceWith to replace the entire div containing 2 select boxes, the replaceWith will first try to add new div before removing the old div.

This causes all 4 select boxes to co-exist in the DOM thus causing event handling mechanism to malfunction (in Firefox). The work around I have found is to do following

node.empty(); node.replaceWith(res.responseText);

It would save a lot of time for someone if this behavior could be changed in the replaceWith function.

Change History

comment:1 Changed 4 years ago by dmethvin

Can you attach a simple test case?

comment:2 Changed 4 years ago by brandon

  • need changed from Review to Test Case
  • Status changed from new to closed
  • Resolution set to invalid

Please attach a complete test case. Are you using clone to create the second div? Cloning form elements has issues cross-browser.

comment:3 Changed 4 years ago by jayfresh

  • Status changed from closed to reopened
  • Resolution invalid deleted

I don't have a way to show you a simple test case right now, but I had this problem too - I am testing in IE6 and FF3.5 and it's only a problem in IE.

I am replacing an input with a constructed drop-down - this is the code ($row.val is pointing at the input):

var $select = this.makeSelect(values);
$row.val.replaceWith($select);

I have found that if I replace 'replaceWith' with the function suggested in #2697, the problem goes away. Here is the code for that function:

$.fn.replaceWith = function(value) {
    return this.each(function() {
    var e = $(this);
    var s = e.next();
    var p = e.parent();
    e.remove();
    if (s.size())
        s.before(value);
    else
        p.append(value);
    });
};

comment:4 Changed 4 years ago by jayfresh

Worth also mentioning that the event handlers on $row didn't fire after a change event on $row.val. With the replacement code in place, they fired.

J.

comment:5 Changed 3 years ago by john

  • Status changed from reopened to closed
  • Resolution set to duplicate

Duplicate of #2697.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.