[jQuery] What is the best way to replace an element using JQuery?
Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuery--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
I would try: $(#myElement).before( divnew element/div).remove(); -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of George Adamson Sent: mercredi 31 janvier 2007 15:02 To: discuss@jquery.com Subject: [jQuery] What is the best way to replace an element using JQuery? Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuer y--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- Ce message Envoi est certifié sans virus connu. Analyse effectuée par AVG. Version: 7.5.441 / Base de données virus: 268.17.17/661 - Date: 30/01/2007 23:30 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
You might try: $(#myElement).remove().after(divnew element\/div); It would be nice to have a replaceWith method and a wrapInner method in the core as I've found I need to be able to do this on a regular basis. FWIW, Shane On 1/31/07, George Adamson [EMAIL PROTECTED] wrote: Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuery--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- - Bender: Amy, you like cute things so I baked you a pony. - http://www.reefs.org - Where reefkeeping begins on the internet. http://www.advancedaquarist.com - High quality, free monthly publication for the reef keeping hobbyist. http://www.aquaristcourses.org - Distance learning courses for the marine aquarist. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
You should be able to do something like this. $('#myElement').before('replacement').remove(); -- Brandon Aaron On 1/31/07, George Adamson [EMAIL PROTECTED] wrote: Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuery--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
This is mostly a guess, but $('#element').after('divnew stuff/div').remove(); andreas On Jan 31, 2007, at 15:02 , George Adamson wrote: Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the- best-way-to-replace-an-element-using-JQuery--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
Thanks for all your replies. Looks like everyone settled on a similar solution using before() or after() in combination with remove(). Yep, a replace() function would be nice. Shane - Just in case you don't already know about it, wrapInner() is available as a plugin at http://motherrussia.polyester.se/jquery/wrapinner/ - Perhaps you are already using it. I agree, these two would be good in the core. Cheers all, George Shane Graber - jQuery wrote: You might try: $(#myElement).remove().after(divnew element\/div); It would be nice to have a replaceWith method and a wrapInner method in the core as I've found I need to be able to do this on a regular basis. FWIW, Shane On 1/31/07, George Adamson [EMAIL PROTECTED] wrote: Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuery--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- - Bender: Amy, you like cute things so I baked you a pony. - http://www.reefs.org - Where reefkeeping begins on the internet. http://www.advancedaquarist.com - High quality, free monthly publication for the reef keeping hobbyist. http://www.aquaristcourses.org - Distance learning courses for the marine aquarist. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuery--tf3148907.html#a8732952 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
Personally i don't think so. Once you get the jquery logic, it's very easy to figure out how to do the replace() (see how many people answered correctly :) ) One of the strength of jquery is its low file size. Only use the extreme necessary for jquery, and plugins for custom flavours... -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of George Adamson Sent: mercredi 31 janvier 2007 18:14 To: discuss@jquery.com Subject: Re: [jQuery] What is the best way to replace an element using JQuery? Thanks for all your replies. Looks like everyone settled on a similar solution using before() or after() in combination with remove(). Yep, a replace() function would be nice. Shane - Just in case you don't already know about it, wrapInner() is available as a plugin at http://motherrussia.polyester.se/jquery/wrapinner/ - Perhaps you are already using it. I agree, these two would be good in the core. Cheers all, George Shane Graber - jQuery wrote: You might try: $(#myElement).remove().after(divnew element\/div); It would be nice to have a replaceWith method and a wrapInner method in the core as I've found I need to be able to do this on a regular basis. FWIW, Shane On 1/31/07, George Adamson [EMAIL PROTECTED] wrote: Despite having used JQuery for some time and also written plugins, I've never discovered a good way to replace one element with another. Just need something like: $(#myElement).replaceWith(divnew element/div) Depending on the circumstances I usually find I have to resort to DOM methods or if myElement has no siblings then I can use .parent().html(...). Any thoughts? Am I missing something?! Cheers George -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-usin g-JQuery--tf3148907.html#a8729470 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- - Bender: Amy, you like cute things so I baked you a pony. - http://www.reefs.org - Where reefkeeping begins on the internet. http://www.advancedaquarist.com - High quality, free monthly publication for the reef keeping hobbyist. http://www.aquaristcourses.org - Distance learning courses for the marine aquarist. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/What-is-the-best-way-to-replace-an-element-using-JQuer y--tf3148907.html#a8732952 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- Ce message Envoi est certifié sans virus connu. Analyse effectuée par AVG. Version: 7.5.441 / Base de données virus: 268.17.17/661 - Date: 30/01/2007 23:30 ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] What is the best way to replace an element using JQuery?
Yep, a replace() function would be nice. Here you go, world's smallest plugin: $.fn.replace = function(o) { return this.after(o).remove(); }; ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/