tags:

views:

3627

answers:

3

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?

+13  A: 

Once a dom element is created, the tag is immutable, I believe. You'd have to do something like this:

$(this).replaceWith($('<h5>' + this.innerHTML + '</h5>');
mishac
That's what I figured, thanks for an even more direct solution than I was going to use.
Chris Cooper
Please see my comment below...changing document structure to apply style is not the best approach.
jrista
A: 

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:

$('#change').click(function(){
  $('p').addClass('emphasis');
});
Dave Ward
The reason I'm asking to change the element/tag is because I am attempting to change a tag (an <h5>, though the type is irrelevant) to an <input> when an "edit" button is clicked elsewhere on the page.
Chris Cooper
+3  A: 

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:

$('a.change').click(function() {
    $('p#changed').css("font-weight", "bold");
});

<p id="changed">Hello!</p>
<a id="change">change</a>
jrista
The reason I'm asking to change the element/tag is because I am attempting to change a tag (an <h5>, though the type is irrelevant) to an <input> when an "edit" button is clicked elsewhere on the page.
Chris Cooper
Even in this case, you shouldn't be modifying your document structure. If you need to display an input in response to an edit butting being clicked, then put the input in and stick display:none or visibility:hidden on it. Hide the <h5> and show the <input> in response to the button click. If you are constantly modifying your document structure...your just asking for a bucketfull of style and layout issues down the road.
jrista
You really think its better to have a whole mess of hidden edit functions that are intended for site admins only rather than modifying the document structure?
Chris Cooper
Absolutely. Your javascript is embedded or linked, so if your concern is security, then your approach to security is somewhat flawed. You should render the content of your document according to the role of the user...blending content for roles isn't a secure way to approach the problem. If someone is logged into your system as an admin, render the content for an admin. If they are logged into your system as a reader, render the content for a reader. This completely eliminates content that shouldn't be accessed. Once the content is rendered, use CSS to style your document and show/hide stuff.
jrista
I agree with you and have taken your recommendations into the project. I'll be using toggle() to show admin elements that are only rendered when an admin is logged in. Although unrelated (directly) to the original question, this is probably a better solution than the direction I was originally going. Cheers!
Chris Cooper
Woo! Bagged another blop of bad security! A win, and rounds for all! (insert beer icon here)
jrista