views:

61

answers:

3

How i can do this:

I generate this error:

<td htmlfor="newPassword" generated="true" class="error">Error</td>

but i want this:

<td htmlfor="newPassword" generated="true" class="error"><span>Error</span></td>

How i can add "span" or other tags for "Error" text?

+1  A: 

try this

$("td.error").each(function(){
var span = $('<span></span>').text($(this).text());
$(this).html(span);
});

this will wrap text inside a span, and add the new html into the TD element.

there might be a shorter version using the .wrap() but this one will work.

Keep in mind that this works only for text. if there is more than just text in the TD you will need to move the HTML inside the span instead of the text.

Sander
+2  A: 

You can use jquery wrapInner():

$("td.error").wrapInner("<span></span>");

this does exactly what you mean.

Hope it helps. Sinan.

Sinan Y.
nice, i knew there woud have been a shorter version than what i suggested, got to remember this wrapInner, i knew wrap but had no idea about the existance of wrapInner thanks
Sander
@Sander, you're welcome, jquery guys thought almost every possible use case. that's why i really like it and prefer it amongst others... My thanks from here to all jQuery community :)
Sinan Y.
A: 

Thanks for quick answers. I do it with this code:

showErrors: function(errorMap, errorList) {

this.defaultShowErrors();

$("td.error").each(function(){
 var span = $('<span></span>').text($(this).text());
 $(this).html(span);
 console.log(this);
});
}

This works, but locate it in "showErrors" is best possible place for this?

then you should do Sander a favor to get him more reputation, by selecting his answer as "selected answer" to your question. cheers.
Sinan Y.