Is this possible?
example:
$('a.change').click(function(){
//code to change p tag to h5 tag
});
<p>Hello!</p>
<a id="change">change</a>
So clicking the change anchor should cause the <p>Hello!</p>
section to change to (as an example) an h5 tag so you'd end up with <h5>Hello!</h5>
after the click. I realize you can delete the p tag and replace it with an h5, but is there anyway to actually modify an HTML tag?
Here's an extension that will do it all, on as many elements in as many ways...
Example usage:
or
or even
Plugin Source:
This is my solution. It allows to toggle between tags.
Is there a specific reason that you need to change the tag? If you just want to make the text bigger, changing the p tag's CSS class would be a better way to go about that.
Something like this:
Rather than change the type of tag, you should be changing the style of the tag (or rather, the tag with a specific id.) Its not a good practice to be changing the elements of your document to apply stylistic changes. Try this:
I noticed that the first answer wasn't quite what I needed, so I made a couple of modifications and figured I'd post it back here.
Improved
replaceTag(<tagName>)
Arguments:
Returns:
Okay, I know there are a few answers here now, but I took it upon myself to write this again.
Here we can replace the tag in the same way we use cloning. We are following the same syntax as .clone() with the
withDataAndEvents
anddeepWithDataAndEvents
which copy the child nodes' data and events if used.Example:
Source:
Once a dom element is created, the tag is immutable, I believe. You'd have to do something like this: