Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Does 'this' not work within a function?
Old 06-20-2010, 09:11 PM Does 'this' not work within a function?
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Hey dudes,

Quick question. I'm making a drag n' drop script from scratch (hard D and this is what I have:

HTML Code:
<script language="javascript">
function getMousePos() {
  if (dragIt==true) {
    mouseX.value = window.event.clientX;
    mouseY.value = window.event.clientY; 
    dragBox.style.top = mouseY.value+"px";
    dragBox.style.left = mouseX.value+"px";
    dragBox.innerHTML="<table height='100%' width='100%' summary='drag'><tr><td style='text-align:center;' valign='center'>Dragging...</td></tr></table>";
  }
  else {dragBox.innerHTML="";}
}
function dragging(divToDrag) {
  dragIt=true;
}
function stopdragging(divToStopDragging) {
  dragIt=false;
}
</script>
<body onmousemove="getMousePos();">
<input type="text" style="display:none;position:absolute;" id="mouseX" style="border:none;">
<input type="text" style="display:none;position:absolute;" id="mouseY" style="border:none;">

<div onselectstart="return false" class="draggable" id="dragBox" onmousedown="dragging(this.id)"  onmouseup="stopdragging(this.id)" style="z-index:1000;position:absolute;background:red;padding:20px;width:100px;height:100px;"></div>
<br /><br /><br /><br />
<div onselectstart="return false" class="draggable" id="dragBox2" onmousedown="dragging(this.id)"  onmouseup="stopdragging(this.id)" style="z-index:1000;position:absolute;background:blue;padding:20px;width:100px;height:100px;"></div>
A lot, I know. Just copy it into the tryit editor :P
But as you can see, getMousePos() is this:
function getMousePos() {
if (dragIt==true) {
mouseX.value = window.event.clientX;
mouseY.value = window.event.clientY;
dragBox.style.top = mouseY.value+"px";
dragBox.style.left = mouseX.value+"px";
dragBox.innerHTML="<table height='100%' width='100%' summary='drag'><tr><td style='text-align:center;' valign='center'>Dragging...</td></tr></table>";
}
else {dragBox.innerHTML="";}
}
But that only will work on the div which id is 'dragBox'. I tried replacing the 'dragBox's with 'this.id', but that doesn't work D:. What am I doing wrong?
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-21-2010, 01:02 PM Re: Does 'this' not work within a function?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
"this" is a reference to the owner of a function/object. So in the context of dragBox 'this' will refer to the correct object.

Outside the dragBox context it would refer to the window or global object.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Does 'this' not work within a function?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10243 seconds with 12 queries