Ticket #8505 (closed bug: invalid)
replaceWith() call despite empty result set from selector
| Reported by: | Jakob Jonas | Owned by: | Jakob Jonas |
|---|---|---|---|
| Priority: | undecided | Milestone: | 1.next |
| Component: | unfiled | Version: | 1.5.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
With this HTML
<body> <div id="XXX">123</div> </body>
executing the following jQuery, the first statement matches and replaces the content with a (missing) image. The second statement does not match its selector but still attempts to load the image even though it is not going to be used
$("123").replaceWith('<img src="http://www.bbc.co.uk/asdasdsdsd.jpg" />');
$("456").replaceWith('<img src="http://www.bbc.co.uk/aasdqerwqeqwe.jpg" />');
If the replaceWith HTML was a trigger to a server side action, this could create havoc for someone whose code works in this way.
Change History
comment:1 Changed 2 years ago by addyosmani
- Owner set to Jakob Jonas
- Status changed from new to pending
comment:2 Changed 2 years ago by addyosmani
- Status changed from pending to closed
- Resolution set to invalid
Please note that as per my original message on this ticket, it looks like you were attempting to use the XXX selector incorrectly. Rather than querying with $("XXX"), please use $("#XXX") if you wish to select based on an ID. Closing this ticket unless a further test case or comments become available.
comment:3 in reply to: ↑ description Changed 2 years ago by Jakob Jonas
Replying to Jakob Jonas: With this HTML
<html> <head> </head> <body> <div id="XXX">123</div> </body> </html>
executing the following jQuery, the first statement matches and replaces the content with a (missing) image. The second statement does not match its selector but still attempts to load the image even though it is not going to be used
$("#XXX").replaceWith('<img src="http://www.bbc.co.uk/asdasdsdsd.jpg" />');
$("#YYY").replaceWith('<img src="http://www.bbc.co.uk/aasdqerwqeqwe.jpg" />');
If the replaceWith HTML was a trigger to a server side action, this could create havoc for someone whose code works in this way.
comment:4 Changed 2 years ago by pettys
Agree with Jakob Jonas - something seems amiss to me, too. When passing a function to replaceWith the problem is even more sinister. See http://jsfiddle.net/xjUWd/2/
Did a little searching and I believe this issue is the same as: http://bugs.jquery.com/ticket/7623
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Thanks for submitting a ticket to the jQuery Bug Tracker. Could you please provide us with a complete test case that reproduces the issue you're experiencing on jsFiddle.net?.
I've just tried here and I'm unsure of whether you're actually trying to treat XXX as a valid selector in $('XXX') or if you're actually treating the XXX as an ID and using # prior to this.
Also, if you're going to attempt to access an element which may or may not exist, you can handle this manually by simply checking for the existence of the element before trying to call replaceWith. In my opinion this is something which should be handled outside jQuery (if absolutely) needed, but we'll await your test case.