HTML and CSS Reference
In-Depth Information
eXerCISe 14-6. aDDING prOMOtION
Add the kingMe() function shown in Listing 14-7 to the existing script element.
Listing 14-7. Implementing the kingMe() function
function kingMe(piece) {
// If we're already a king, just return
if (, 1) === "W" ||, 1) === "B")
var newPiece;
// If this is a white piece on the 7th row
if (, 1) === "w" &&, 1) === "7") {
newPiece = document.createElement("img");
newPiece.src = "Images/WhiteKing.png"; = "W" +, 2);
// If this is a black piece on the 0th row
if (, 1) === "b" &&, 1) === "0") {
var newPiece = document.createElement("img");
newPiece.src = "Images/BlackKing.png"; = "B" +, 2);
// If a new piece was created, set its properties and events
if (newPiece) {
newPiece.draggable = true;
newPiece.addEventListener('dragstart', dragStart, false);
newPiece.addEventListener('dragend', dragEnd, false);
var parent = piece.parentNode;
The kingMe() function simply returns if the id prefix is ether “B” or “W”, which
indicates this is already a king. It then checks to see if this is a white piece on row 7
or a black piece on row 0. If so, a new img element is created with the appropriate
src and id properties. If a new img was created the function then sets all of the
properties and events, removes the existing img element from the div element and
adds the new one.
Modify the drop() function to call the kingMe() function after a drop has been
performed by adding the line shown in bold.
Search WWH ::

Custom Search