Practice Set Week 11, PS #2

by subsari

HTML

<h4>Practice Set: Basic jQuery</h4>

<p>This assignment will give you practice with some basic jQuery capabilities. There are three tasks in this practice set. Use the jQuery API documentation as needed!  </p>
    <p>
    Your tasks are:</p>
<ol id="tasks">
    <li id="firstItem">Find the items in this list and add a yellow background to them (consider using the .css() method)</li>
    <li id="secondItem">Find the second item in this list (this one!), and write its HTML content to the third item (overwriting the third one's content)
    </li>
    <li id="thirdItem">This one gets overwritten with the content of #2 (the .html() method may be helpful here, for both reading from #2 and overwriting #3)</li>
    <li id="fourthItem">Make this item disappear if you click on it! (the .click() method may be helpful)</li>
</ol>

JavaScript

// your solutions here
addYelloBackground(); // 1. find items and add yellow backgroud
replaceThirdItem(); // 2. replace third item

function addYelloBackground(){
	$("li").each(function(index){
		$(this).css("background", "yellow");
	});
}

function replaceThirdItem(){
	var source = null; 
	var destination = null;
	
	$("li").each(function(index){
		if (index == 1) source = $(this); // assign 2nd item
		if (index == 2) destination = $(this); // assign 3rd item
		if (index == 3) addClickToDisappear($(this)); // #4 add event listener
	});
	
	$(destination).html($(source).html());
}

// utility function click to disappear
function addClickToDisappear(obj){
	$(obj).click(function(){
		$(this).remove();
	});
}