I have adapted code from http://dunnbypaul.net/js_mouse/
I want to use a button to create new draggable divs but i keep getting error "is null or not an object"
heres the code - <html>
-
<head>
-
<title>Drag and drop module</title>
-
<style type="text/css">
-
#canvas {
-
margin:auto;
-
padding:0px;
-
position: absolute;
-
background-color:grey;
-
border:dashed gray 1px;
-
top: 5px;
-
margin:auto;
-
padding:0px;
-
width:100%;
-
height:400px;
-
float:left;
-
font-size: 8px;
-
font-family:Arial, Helvetica, sans-serif;
-
overflow: hidden;
-
}
-
#controls {
-
margin:auto;
-
padding:10px;
-
position: absolute;
-
width:100%;
-
top:415px;
-
float:left;
-
background: grey;
-
border:dashed black 1px;
-
height: 150px;
-
}
-
#newObject {
-
position: absolute;
-
left: 10px;
-
top: 10px;
-
background-color:green;
-
border:solid gray 1px;
-
margin:10px;
-
padding:4px;
-
width:50px;
-
height:50px;
-
float:left;
-
zIndex:1;
-
cursor:pointer;
-
font-size: 8px;
-
font-family:Arial, Helvetica, sans-serif;
-
}
-
</style>
-
<script type = "text/javascript">
-
function createObject (){
-
cv = document.getElementById("canvas");
-
var newObject = document.createElement('div');
-
newObject.Class = "newObject";
-
newObject.id = "newObject";
-
cv.appendChild(newObject);
-
newObject.onmousedown=grab(this);
-
-
}
-
-
var mousex = 0;
-
var mousey = 0;
-
var grabx = 0;
-
var graby = 0;
-
var orix = 0;
-
var oriy = 0;
-
var elex = 0;
-
var eley = 0;
-
var algor = 0;
-
-
var dragobj = null;
-
-
function falsefunc() { return false; } // used to block cascading events
-
-
function init()
-
{
-
document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
-
update();
-
}
-
-
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
-
{
-
if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
-
-
if (e)
-
{
-
if (e.pageX || e.pageY)
-
{ // this doesn't work on IE6!! (works on FF,Moz,Opera7)
-
mousex = e.pageX;
-
mousey = e.pageY;
-
algor = '[e.pageX]';
-
if (e.clientX || e.clientY) algor += ' [e.clientX] '
-
}
-
else if (e.clientX || e.clientY)
-
{ // works on IE6,FF,Moz,Opera7
-
mousex = e.clientX + document.body.scrollLeft;
-
mousey = e.clientY + document.body.scrollTop;
-
algor = '[e.clientX]';
-
if (e.pageX || e.pageY) algor += ' [e.pageX] '
-
}
-
}
-
}
-
-
function update(e)
-
{
-
getMouseXY(e); // NS is passing (event), while IE is passing (null)
-
-
document.getElementById('xpos').value = elex;
-
document.getElementById('ypos').value = eley;
-
document.getElementById('object').value = dragobj ? (dragobj.id ? dragobj.id : 'unnamed object') : '(null)';
-
}
-
-
function grab(context)
-
{
-
document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
-
dragobj = context;
-
dragobj.newObject.zIndex = 10; // move it to the top
-
document.onmousemove = drag;
-
document.onmouseup = drop;
-
grabx = mousex;
-
graby = mousey;
-
elex = orix = dragobj.offsetLeft;
-
eley = oriy = dragobj.offsetTop;
-
update();
-
}
-
-
function drag(e) // parameter passing is important for NS family
-
{
-
if (dragobj)
-
{
-
elex = orix + (mousex-grabx);
-
eley = oriy + (mousey-graby);
-
dragobj.newObject.position = "absolute";
-
dragobj.newObject.left = (elex).toString(10) + 'px';
-
dragobj.newObject.top = (eley).toString(10) + 'px';
-
}
-
update(e);
-
return false; // in IE this prevents cascading of events, thus text selection is disabled
-
}
-
-
function drop()
-
{
-
if (dragobj)
-
{
-
dragobj.style.zIndex = 0;
-
dragobj = null;
-
}
-
update();
-
document.onmousemove = update;
-
document.onmouseup = null;
-
document.onmousedown = null; // re-enables text selection on NS
-
}
-
-
</script>
-
</head>
-
<body onload="init();">
-
<div id = "canvas">
-
</div>
-
<div id = "controls">
-
<input type = "button" value = "New Object" onClick = "createObject()">
-
<input type = "text" value = "" name = "xpos" id = "xpos">
-
<input type = "text" value = "" name = "ypos" id = "ypos">
-
<input type = "text" value = "" name = "object" id = "object">
-
-
</div>
-
</body>
-
</html>
6 4929
sorry just to be a little clearer.
Error:
Line: 116
Char: 3
error: 'dragobj.newObject' is null or not an object
code:0
"If you can solve this your a genius!!" is a brilliant title :D, but doesn't really describe the problem, so I've changed it.
Re. your problem: dragobj.newObject is incorrect. Replace newObject with style. See if that makes a difference.
yeah I have already tried that. I think the problem lies in the createObject function. Seems to be a problem assigning the onmousedown event.
Yeah, that should be: - newObject.onmousedown=function(){grab(this);}
Yeah, that should be: - newObject.onmousedown=function(){grab(this);}
Has anyone every told you you're some pup! :)
Thanks... you don't know how much that has helped
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Don Vaillancourt |
last post by:
What's going on with Javascript.
At the beginning there was the "undefined" value which represented an
object which really didn't exist then came the null keyword. But
yesterday I stumbled...
|
by: Lauchlan M |
last post by:
Hi.
Usin ASP.NET, getting an "Object reference not set to an instance of an
object" error.
In my login.aspx page I have:
string arrUserRoles = new string {"UserRole"};...
|
by: Karel Vandenhove |
last post by:
Hi,
I get an error "cannot apply indexing with to an expression of type
object" when I try to compile the code below.
SSLScannerManager is a COM component. (Used to access fingerprint...
|
by: Christian Hvid |
last post by:
What is the easiest way to get the "row object" or "item object" when a
datagrid is clicked?
I have web form with a datagrid. And I have an array of something called
BlogEntry that I bind to the...
|
by: Joe Van Dyk |
last post by:
When you delete a pointer, you should set it to NULL, right?
Joe
|
by: louie.hutzel |
last post by:
This JUST started happening, I don't remember changing any code:
When I click the submit button on my form, stuff is supposed to happen
(which it does correctly) and a result message is posted back...
|
by: JHNielson |
last post by:
I have a simple question, but I can't seem to find the answer.
I have this code to count the number of records that pass through a set of processes:
It counts how many records go through...
|
by: =?Utf-8?B?QmFkaXM=?= |
last post by:
I'm doing a server side automation(I know it's bad but..) and its working
fine locally and when accessing it from a remote machine using web browser is
was giving me this error"Retrieving the COM...
|
by: piyumi80 |
last post by:
hi,
i wrote the following code to get a specific data row from the data set.but it generates the
"Object reference not set to an instance of an object.".....error
private void...
|
by: RMWChaos |
last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |