Basic idea of my if else statements is this:
- If
.wrapis parent element: do something - Else: do something else
This might not make much sense but if you look at the code here:
http://jsfiddle.net/C6NQM/ ..it might
if( $('.wrap:parent'); )
{ $('.wrap').children().wrap('<div class="new" />'); }
else { $('.wrap').wrap('<div class="new" />'); }
Things work separately if i remove if and else, so i came to the conclusion that there’s something wrong with my if condition.
Any ideas about what’s wrong here?
I think you want:
Updated fiddle.
Comments on your code:
if( $('.wrap:parent'))will always evaluate totrue. A call to jQuery always returns a jQuery object. You could doif( $('.wrap:parent').length > 0), but even then you would select all elements in your example, as:parentselects also nodes that have text nodes as children.But even with that, the whole statement will work as follows: If there are any
.wrapelements that are parents, wrap their children in a new element. If not, wrap the elements themselves.So it will not treat every
.wrapelement individually.Update: To cover every case (children, text content, empty), you probably have to do: