1

Say I have an element like so:

<div id="color1" foo="test" class="yes">Hello</div>

I want to turn it into:

<span id="color1" foo="test" class="yes">Hello</span>

I thought to use wrap/unwrap, but not quite getting what I need.

user1492442
  • 239
  • 3
  • 13
  • 1
    http://stackoverflow.com/questions/14645806/get-all-attributes-of-an-element-using-jquery – MattSizzle Aug 05 '14 at 02:11
  • 1
    Thanks Matt, that isn't exactly what I am looking for. I don't necessarily want the attributes. I just want to create a new element and make sure the integrity of the attributes are intact. thanks though. – user1492442 Aug 05 '14 at 02:15
  • 1
    @Ker cause I don't have control of the initial elements. That is why I need to convert them on the fly. – user1492442 Aug 05 '14 at 02:16
  • 3
    what exactly does "make sure the integrity of the attributes are intact" mean? Either you want to copy the attributes or you don't, you can't create a new element and magically have the same attributes without copying them ? – adeneo Aug 05 '14 at 02:21
  • I agree with @adeneo. Also, an attribute like `id` can't be duplicated on a page. No two elements can have the same `id`, so what should be done in this situation? – Adam Merrifield Aug 05 '14 at 02:31
  • what exactly do you want to do with the new element? replace original? Comments made make it very confusing since other answer has all the important attribute information – charlietfl Aug 05 '14 at 02:34
  • 1
    possible duplicate of [how to change an element type using jquery](http://stackoverflow.com/questions/8584098/how-to-change-an-element-type-using-jquery) – scrowler Aug 05 '14 at 02:45
  • 1
    Here's another way to do it -> **http://jsfiddle.net/p479G/** – adeneo Aug 05 '14 at 03:07
  • 1
    possible duplicate of [Change tag using javascript](http://stackoverflow.com/questions/13389751/change-tag-using-javascript) – wedi Aug 05 '14 at 03:40
  • @adeneo - clever boy you are! lol. That is nice. – user1492442 Aug 05 '14 at 04:45

1 Answers1

2

Edit, removed g modifier from RegExp

var _s = $("div#color1").clone()[0].outerHTML.replace(/div/, "span");
$("div#color1").replaceWith(_s);

jsfiddle http://jsfiddle.net/guest271314/E53tN/

guest271314
  • 1
  • 15
  • 104
  • 177