5. Why not use the var we created in the previous exercise?


#1



I would like to use this:
 $(document).ready(function() { 
    $('#one').after('<p>Whatever</p>');
    var $p = $('p');
    $("#two").after($p);
    $p.remove();
    });

rather than this:
$(document).ready(function() { 
    $('#one').after('<p>Whatever</p>');
    var $p = $('p');
    $("#two").after($p);
    $('p').remove();
    });


Why can't I use the var we created before?


#2

can you give me your html code?

@olouthe


#3

@olouthe I think both should work. When you tried it, did it work and you just didn't get passed, or did the first code actually not work?


#4

<!DOCTYPE html>
<html>
    <head>
        <title>Result</title>
        <script type='text/javascript' src='script.js'></script>
    </head>
    <body>
        <div class="container">
            <h2>Greetings</h2>
            <div id="one">Div #1</div>
            <div id="two">Div #2</div>
        </div>   
    </body>
</html>

#5

Oops, try again.
Make sure you remove your <p> using the .remove() function.


#6

i'm sorry everyone for the messy posts. i just copy/paste but it doesnt seem to appear in my answers. Anyway thank you for your help. It was mostly a question whether i could use a var rather than the stuff itself. if you say it should work then something else must be wrong in my code. thank you


#7

@olouthe No, I think your code is fine, the problem is with the SCT (Submission Correctness Test; code checker) for this exercise; it's looking at your code and not seeing what it expects, so even though your code works the exercise throws an error :slight_smile: