By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

delete an object

100+
P: 178
Can anyone help me. I need a function to delete an object. Here is the function I use to create objects.

Expand|Select|Wrap|Line Numbers
  1. function showObject (){
  2.     if (request.readyState == 4) {
  3.     var returned = request.responseText;
  4.     var splitResult = returned.split(" ");
  5.     var h = splitResult[0];
  6.     var w = splitResult[1];    // the dimensions must be set to a scale as they are to big for the screen. 25px represents 100mm
  7.     h = h/5;
  8.     w = w/5;
  9.  
  10.     cv = document.getElementById("canvas");
  11.     newObject = document.createElement('div');
  12.     newObject.Class = g_objName;
  13.     newObject.id = "newObject";
  14.     newObject.innerHTML = g_objName;
  15.     newObject.alt = g_objName;
  16.     newObject.style.height = h;
  17.     newObject.style.width = w;
  18.     newObject.onmousedown=function(){grab(this);}    
  19.     cv.appendChild(newObject);
  20. }
  21. }
  22.  
Jul 30 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Use removeChild().
Jul 30 '08 #2

100+
P: 178
Yeah but how do I removeChild where newObject.innerHTML is a specified value?
Jul 30 '08 #3

acoder
Expert Mod 15k+
P: 16,027
If you have a number of divs, use getElementsByTagName("div") on the canvas and check the contents of each one.
Jul 30 '08 #4

100+
P: 178
I can get the elements by tagname but I cant get at the one I want. Can anyone advise? Heres what ive been trying

Expand|Select|Wrap|Line Numbers
  1. var arr = new Array(); arr = document.getElementsByTagName("div");  //make array of all divs with id = newObject
  2.     // alert("Total Number of HTML Elements Found: " + document.documentElement.getElementsByTagName("div").length); 
  3.     for(var i=0; i < arr.length; i++) 
  4.         {
  5.             //check innerHTML against obj_name
  6.             alert("innerHTML" + document.documentElement.getElementsByTagName("*").item(i).nodeName);
  7.         }
Jul 31 '08 #5

acoder
Expert Mod 15k+
P: 16,027
Use indexOf to find the specified string. You would against arr[i].innerHTML in the loop.

You can just get the div elements within the canvas element rather than the whole page if you use cv.getElementsByTagName("div") where cv is the canvas element.

Just a note that IDs should be unique. You can't have the same ID for more than one element in a page.
Jul 31 '08 #6

100+
P: 178
changed the id and was able to do it quite simply. thanks
Jul 31 '08 #7

acoder
Expert Mod 15k+
P: 16,027
Oh yes, of course, if you make each element unique, that pretty much solves your problem. Good to hear that it's sorted.
Jul 31 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.