Connecting Tech Pros Worldwide Forums | Help | Site Map

delete an object

Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#1: Jul 30 '08
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.  

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 30 '08

re: delete an object


Use removeChild().
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#3: Jul 30 '08

re: delete an object


Yeah but how do I removeChild where newObject.innerHTML is a specified value?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jul 30 '08

re: delete an object


If you have a number of divs, use getElementsByTagName("div") on the canvas and check the contents of each one.
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#5: Jul 31 '08

re: delete an object


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.         }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Jul 31 '08

re: delete an object


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.
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#7: Jul 31 '08

re: delete an object


changed the id and was able to do it quite simply. thanks
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Jul 31 '08

re: delete an object


Oh yes, of course, if you make each element unique, that pretty much solves your problem. Good to hear that it's sorted.
Reply