Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I wnat to use the script given belo

Status
Not open for further replies.

navosavo

Technical User
Jul 13, 2002
5
GB
I wnat to use the script given below, but instead of using letters I want to use images. But when I replace letters with images the function
function TestLetter(l) {
if (elDragged.innerText==l+" ")
alert('Good Move')
else
alert('Try again!')
doesn't respond correctly.

I'm a novice at this and would appreciate someone guiding me.

THE SCRIPT AS IT IS
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
/* The drag-drop framework is copyright 1997-98 insideDHTML.com, LLC.
This code can be reused as long as this entire comment is not removed.
For more information, see */

// Event handlers for demonstration

function doOverTarget() {
elDragged.overTarget.style.background=&quot;lightgreen&quot;
}

function doOutTarget() {
elDragged.overTarget.style.background=&quot;yellow&quot;
}

function TestLetter(l) {
if (elDragged.innerText==l+&quot; &quot;)
alert('Good Move')
else
alert('Try again!')
}
</SCRIPT>

<SCRIPT LANGUAGE=&quot;JavaScript&quot; ID=code>
var elDragged = null // Track drag-source in global variable.

function testOver(iLeft, iTop) {
// Test if the element is over a valid drop-target
var hit = false
if (elDragged.getAttribute(&quot;dropTarget&quot;)!=null) {
for (var intLoop=0; intLoop<elDragged._targets.length; intLoop++) {
// Check all drop-targets
var el = elDragged._elTargets[intLoop]
if (null!=el) {
if ((iTop > el._top) && (iLeft > el._left) && (iLeft+elDragged.offsetWidth < el.offsetWidth+el._left) && (elDragged.offsetHeight+iTop < el.offsetHeight+el._top)) {
elDragged.overTarget= el
var hit=true
elDragged._over[intLoop] = true
// Fire events when over
if (event.type==&quot;mouseup&quot;) {
if (el.getAttribute(&quot;onDropTarget&quot;)!=null)
eval(el.getAttribute(&quot;onDropTarget&quot;));
} else
{
if (el.getAttribute(&quot;onOverTarget&quot;)!=null)
eval(el.getAttribute(&quot;onOverTarget&quot;))
}
}
else {
// Fire event when leaving
if ((el.getAttribute(&quot;onOutTarget&quot;)!=null) && (elDragged._over[intLoop])) {
elDragged.overTarget= el
eval(el.getAttribute(&quot;onOutTarget&quot;))
}
elDragged._over[intLoop] = false
}
}
}
}
return hit;
}

function doMouseUp() {
if (elDragged!=null)
if (testOver(elDragged._left, elDragged._top)) {
// Fire event on drag-source if dropped on target
if (elDragged.getAttribute(&quot;onTarget&quot;))
eval(elDragged.getAttribute(&quot;onTarget&quot;))
}
else
if (elDragged.getAttribute(&quot;onNoTarget&quot;))
eval(elDragged.getAttribute(&quot;onNoTarget&quot;))
// Reset global variable
elDragged=null
}

function doMouseMove() {
// Check if mouse button is down and if an element is being dragged
if ((1 == event.button) && (elDragged != null)) {
// Set new position
var intTop = event.clientY+document.body.scrollTop;
var intLeft = event.clientX + document.body.scrollLeft;
elDragged.style.pixelTop = intTop - elDragged._lessTop - elDragged.y;
elDragged.style.pixelLeft = intLeft - elDragged._lessLeft - elDragged.x;
// Cache updated info
elDragged._left = elDragged._lessLeft + elDragged.offsetLeft + document.body.scrollLeft
elDragged._top = elDragged.offsetTop+ elDragged._lessTop +document.body.scrollTop
// Test if over target
testOver(elDragged._left, elDragged._top)
event.returnValue = false;
};
};

function checkDrag(elCheck) {
// Check if the clicked inside an element that supports dragging
// This allows rich HTML in drag-source
while (elCheck!=null) {
if (null!=elCheck.getAttribute(&quot;dragEnabled&quot;))
return elCheck
elCheck = elCheck.parentElement
}
return null
}

function doMouseDown() {
// On the mouse down, test if element can be dragged

var elCurrent = checkDrag(event.srcElement)
// For draggable elements, cache all calculations up front. This is for performance.
if (null!=elCurrent) {
elDragged = elCurrent;
// Determine where the mouse is in the element
elDragged.x = event.offsetX
elDragged.y = event.offsetY
// Make sure we are using pixel units everywhere
elDragged.style.top = elDragged.offsetTop + &quot;px&quot;
elDragged.style.left = elDragged.offsetLeft + &quot;px&quot;
var op = event.srcElement
// Find real location in respect to element being dragged.
if ((elDragged!=op.offsetParent) && (elDragged!=event.srcElement)) {
while (op!=elDragged) {
elDragged.x+=op.offsetLeft
elDragged.y+=op.offsetTop
op=op.offsetParent
}
}

// Move the element
// Where the mouse is in the document
// Calculate what element the mouse is really in
var intLessTop = 0; var intLessLeft = 0;
var elCurrent = elDragged.offsetParent;
while (elCurrent.offsetParent!=null) {
intLessTop+=elCurrent.offsetTop;
intLessLeft+=elCurrent.offsetLeft;
elCurrent = elCurrent.offsetParent;
}
elDragged._lessTop = intLessTop
elDragged._lessLeft = intLessLeft
// Calculate and cache target information
if (null!=elDragged.getAttribute(&quot;dropTarget&quot;)) {
elDragged._targets = elDragged.getAttribute(&quot;dropTarget&quot;).split(&quot;,&quot;)
elDragged._over = new Array
elDragged._elTargets = new Array
for (var intLoop=0; intLoop < elDragged._targets.length; intLoop++) {
var el = document.all[elDragged._targets[intLoop]]
if (null!=el) {
elDragged._elTargets[intLoop]= el;
var intLessTop = el.offsetTop; var intLessLeft = el.offsetLeft;
var elCurrent = el.offsetParent;
var intTop = document.body.scrollTop;
var intLeft = document.body.scrollLeft;
while (elCurrent.offsetParent!=null) {
intLessTop+=elCurrent.offsetTop;
intLessLeft+=elCurrent.offsetLeft;
elCurrent = elCurrent.offsetParent;
}
el._top = intLessTop + intTop
el._left = intLessLeft + intLeft
}
}
}
}
}

function doDragTest() {
// Don't start text selections in dragged elements.
return (null==checkDrag(event.srcElement) && (elDragged==null))
}

// Process all mouse events.
document.onmousedown = doMouseDown;
document.onmousemove = doMouseMove;
document.onmouseup = doMouseUp;
document.ondragstart = doDragTest
document.onselectstart = doDragTest;
</SCRIPT>


<CENTER>
<SPAN &quot;POSITION: RELATIVE; BORDER: 1pt black solid; width: 36em; height: 7em; text-align: center; margin-bottom: 10pt&quot;>
<P>Unscramble the URL of your favorite DHTML Web-Site</P>
<Span STYLE=&quot;top: 2em; cursor: hand; left: 8.5em; width: .8em; height: .8em; background: lightgreen; color: navy; text-align: center; position: absolute&quot; dragEnabled dropTarget=&quot;dropHere,dropHere2,dropHere3,dropHere4,dropHere5,dropHere6&quot;>
I
</Span>
<Span STYLE=&quot;top: 2em; cursor: hand; left: 7.0em; width: .8em; height: .8em; background: lightgreen; color: navy; text-align: center; position: absolute&quot; dragEnabled dropTarget=&quot;dropHere,dropHere2,dropHere3,dropHere4,dropHere5,dropHere6&quot;>
E
</Span>
<Span STYLE=&quot;top: 2em; cursor: hand; left: 10em; width: .8em; height: .8em; background: lightgreen; color: navy; text-align: center; position: absolute&quot; dragEnabled dropTarget=&quot;dropHere,dropHere2,dropHere3,dropHere4,dropHere5,dropHere6&quot;>
S
</span>
<Span STYLE=&quot;top: 2em; cursor: hand; left: 11.5em; width: .8em; height: .8em; background: lightgreen; color: navy; text-align: center; position: absolute&quot; dragEnabled dropTarget=&quot;dropHere,dropHere2,dropHere3,dropHere4,dropHere5,dropHere6&quot;>
I
</span>
<Span STYLE=&quot;top: 2em; cursor: hand; left: 13em; width: .8em; height: .8em; background: lightgreen; color: navy; text-align: center; position: absolute&quot; dragEnabled dropTarget=&quot;dropHere,dropHere2,dropHere3,dropHere4,dropHere5,dropHere6&quot;>
D
</span>
<Span STYLE=&quot;top: 2em; cursor: hand; left: 14.5em; width: .8em; height: .8em; background: lightgreen; color: navy; text-align: center; position: absolute&quot; dragEnabled dropTarget=&quot;dropHere,dropHere2,dropHere3,dropHere4,dropHere5,dropHere6&quot;>
N
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:3.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
W
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:4.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
W
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:6.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
W
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:7.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
.
</span>
<SPAN ID=dropHere STYLE=&quot;position: absolute; top: 5em; left:9em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot; onOverTarget=&quot;doOverTarget()&quot; onOutTarget=&quot;doOutTarget()&quot; onDropTarget=&quot;TestLetter('I')&quot;>
</SPAN>
<SPAN ID=dropHere2 STYLE=&quot;position: absolute; top: 5em; left:10.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot; onOverTarget=&quot;doOverTarget()&quot; onOutTarget=&quot;doOutTarget()&quot; onDropTarget=&quot;TestLetter('N')&quot;>
</span>
<SPAN ID=dropHere3 STYLE=&quot;position: absolute; top: 5em; left:12.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot; onOverTarget=&quot;doOverTarget()&quot; onOutTarget=&quot;doOutTarget()&quot; onDropTarget=&quot;TestLetter('S')&quot;>
</span>
<SPAN ID=dropHere4 STYLE=&quot;position: absolute; top: 5em; left:13.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot; onOverTarget=&quot;doOverTarget()&quot; onOutTarget=&quot;doOutTarget()&quot; onDropTarget=&quot;TestLetter('I')&quot;>
</span>
<SPAN ID=dropHere5 STYLE=&quot;position: absolute; top: 5em; left:15.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot; onOverTarget=&quot;doOverTarget()&quot; onOutTarget=&quot;doOutTarget()&quot; onDropTarget=&quot;TestLetter('D')&quot;>
</span>
<SPAN ID=dropHere6 STYLE=&quot;position: absolute; top: 5em; left:16.5em; width: 1.5em; height: 1.5em; z-index: -1; background: yellow; border: 1pt black solid&quot; onOverTarget=&quot;doOverTarget()&quot; onOutTarget=&quot;doOutTarget()&quot; onDropTarget=&quot;TestLetter('E')&quot;>
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:18.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot; >
D
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:19.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
H
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:21.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
T
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:22.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
M
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:24.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
L
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:25.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
.
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:27.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
C
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:28.5em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
O
</span>
<Span STYLE=&quot;position: absolute; top: 5em; left:30.0em; width: 1.5em; height: 1.5em; border: 1pt black solid; z-index: -1; background: yellow&quot;>
M
</span>
</CENTER>
</span>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top