Labs
Key Logger¶
-
Create files named
keylogger.jsandkeylogger.html. -
Add a single div element to your HTML document,
<div id="content"></div>. -
Add an event listener to your
.jsfile that prints the word 'Down' to console when a 'keydown' event is observed. -
Refactor your event listener to print the keyboard character that was pressed to the console. Hint: use the event object's
keyproperty to determine which key has been pressed. -
Write a function that takes a string as an argument (the character pressed), selects the
#contentdiv on the page, and appends the character to the end of the div'stextContent. Call this function each time your key events are triggered. -
Refactor your event listener to observe backspacing. As of now when the delete is pressed it will print
Backspaceon the screen. Use conditional logic to determine if the delete button has been pressed and to alter the default behavior.
Think string manipulation. How could you remove the last character of a string?
Detailed View¶
-
Create two files
detail.htmlanddetail.js. -
In your html file stub out the basic html structure. In the body of the document add the following:
<div id= "detail"> <p id="title"></p> <p id="company"></p> <p id="salary"></p> </div> <h4>Jobs:</h4> <ul> <li id="0" class="list"></li> <li id="1" class="list"></li> <li id="2" class="list"></li> <li id="3" class="list"></li> <li id="4" class="list"></li> </ul> -
In your
detail.jsfile create an array of objects. Each object should havetitle,company, andsalaryproperties. Feel free to use the following examplelet jobs = [ { company : "Amazon", title : "UX/UI Developer", salary : 72000 }, { company : "IBM", title : "Software Engineer", salary : 84000 }, { company : "Home Advisor", title : "Jr. Java Developer", salary : 65000 }, { company : "Fidelity", title : "Senior Software Engineer", salary : 137000 }, { company : "Google", title : "IT Help Desk", salary : 48000 } ]; -
Select all of the
lis from the DOM using the providedclassattribute, and store them in a variable. Iterate over the collection ofHTMLElementsand set theirtextContentto thecompanyof the corresponding object in the array. At this point the list should be populated and display all of the company names. -
Using the same loop you used in the previous step, add an eventListener to these
lis that when clicked prints out thetextContentof the element as well as its id.Hint: Use
e.targetto access the clicked element'sHTMLElementobject. Just like there is atextPropertyon this object, there is also anidproperty. The provided id's correspond to the indexes of the array we created early. This will be useful soon. -
Refactor your event listener to use the provided
idto access the corresponding object in the array. Once you have access to the object, change the title, company, and salary<p>tagtextContents to represent the information from the selected object.Hint: The id of each
licorresponds to its index in the jobs array.1. JavaScript also lets us alter styles of selected elements.<div id= "detail"> <p id="title"></p> <p id="company"></p> <p id="salary"></p> </div>target.style.property = valuewould set the style of the specified property. Add a line that would change the background-color (backgroundColor) of the clickedli,e.target, to a light green color. -
With each click it turns the background color of the selected element green, but it does not turn the previously selected items back to white. Before the line where you change the
backgroundColorofe.target, loop over all thelielements and turn each one of their backgrounds white.
Now with each click it will reset all of the backgrounds before changing just the one you most recently clicked on.