jQuery - Exercise

Find the flags

by Ryan Morris

HTML

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<div id="container">
     <h1>Find the flag elements.</h1>

    <p>There are three flags ("FLAG") in the content below. Find them and move their containing elements into the #bucket element.</p>
    
    <div class="main">
        <ul>
            <li>One</li>
            <li>FLAG #1</li>
            <li>Three</li>
        </ul>
        <div id="articles">
            <article class="new">
                 <h3>Article 1</h3>

                <p>This is <a href="#">some</a> body content</p>
                <p>This is some body content <a class="flag" href="#">FLAG 2</a>
                </p>
            </article>
            <article class="new">
                 <h3>Article 2</h3>

                <p>This is some body content</p>
            </article>
            <article>
                 <h3>Article 3</h3>

                <p>This is some body content</p>
            </article>
            <div class="footer">
                <div>
                    <div>
                        <div></div>
                        <div>
                            <div>FLAG 3</div>
                            <div></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div id="bucket">
         <h2>Dropped Flags:</h2>

        <!-- Where are my flags? -->
    </div>
</div>

CSS

body{
    font-size:.7em;
}

#bucket {
    position:absolute;
    top:5px;
    right:5px;
    border:2px solid #ccc;
    border-radius:2px;
    border-top:0;
    background-color:#ececec;
    width:20%;
    font-size:8px;
}
#bucket::before {
    content:"Bucket";
}

#bucket a,
#bucket div, 
#bucket li{
    display:block;
    color:red;
}

JavaScript

// Hunting flags in the Document
//
// Using jQuery
//
// Find the three elements that have the text "FLAG #" inside of them. 
// Move each of the elements to the "#bucket" element.
//
// Hint: Expect to use
//     $(selector)
//     .append()
//     .prepend()

// Note: I'm wrapping this in the DOM on-ready event for you
// So that it runs AFTER the page is loaded and ready
$(function() {

    $bucket = $('#bucket');

    // todo: get FLAG 1
    $('.main li:nth-child(2)').appendTo($bucket);
    
    // todo: get FLAG 2
       $('.flag').appendTo($bucket);

    
    // todo: get FLAG 3
    $('.footer div div div:nth-child(2) div').appendTo($bucket);
});
    
// bonus: write a function to crawl the entire document and search for the "FLAG" text, and do the job for you.