Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5958937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:36:51+00:00 2026-05-22T18:36:51+00:00

Basic idea of my if else statements is this: If .wrap is parent element:

  • 0

Basic idea of my if else statements is this:

  • If .wrap is 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:36:52+00:00Added an answer on May 22, 2026 at 6:36 pm

    I think you want:

    $('.wrap').each(function() {
        if($(this).children().length > 0) {
            $(this).children().wrap('<div class="new" />'); 
        }
        else { 
            $(this).wrap('<div class="new" />'); 
        }
    });
    

    Updated fiddle.

    Comments on your code:

    if( $('.wrap:parent')) will always evaluate to true. A call to jQuery always returns a jQuery object. You could do if( $('.wrap:parent').length > 0), but even then you would select all elements in your example, as :parent selects also nodes that have text nodes as children.

    But even with that, the whole statement will work as follows: If there are any .wrap elements that are parents, wrap their children in a new element. If not, wrap the elements themselves.
    So it will not treat every .wrap element individually.

    Update: To cover every case (children, text content, empty), you probably have to do:

    $('.wrap').each(function() {
        if($(this).children().length > 0) {
            $(this).children().wrap('<div class="new" />'); 
        }
        else if($(this).text().length > 0) { 
            $(this).html('<div class="new">' + $(this).text() + '</div>'); 
        }
        else {
           $(this).wrap('<div class="new" />'); 
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basic idea behind a Cancel button is to enable closing your window with
The basic idea of a masterpage is simple -- you have a block of
The basic idea is to take a string and evaluate it against an XML
Here's the basic idea: There is a java window (main) that opens another java
Binary search is harder to implement than it looks. Although the basic idea of
The basic problem is like this: A subscriber has successfully replicated a row from
i'd like a basic idea of how to go about formatting the following paging
I'm sorry this question is going to be a bit vague because I'm not
I am designing a GUI that has the following basic idea (similarly modeled after
Basic requests are: human readable / text format (for easy version control) online (for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.