delete an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cleary1981
    New Member
    • Jun 2008
    • 178

    delete an object

    Can anyone help me. I need a function to delete an object. Here is the function I use to create objects.

    Code:
    function showObject (){
    	if (request.readyState == 4) {
    	var returned = request.responseText;
    	var splitResult = returned.split(" ");
    	var h = splitResult[0];
    	var w = splitResult[1];	// the dimensions must be set to a scale as they are to big for the screen. 25px represents 100mm
    	h = h/5;
    	w = w/5;
    		
    	cv = document.getElementById("canvas");
    	newObject = document.createElement('div');
    	newObject.Class = g_objName;
    	newObject.id = "newObject";
    	newObject.innerHTML = g_objName;
    	newObject.alt = g_objName;
    	newObject.style.height = h;
    	newObject.style.width = w;
    	newObject.onmousedown=function(){grab(this);}	
    	cv.appendChild(newObject);
    }
    }
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use removeChild().

    Comment

    • cleary1981
      New Member
      • Jun 2008
      • 178

      #3
      Yeah but how do I removeChild where newObject.inner HTML is a specified value?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If you have a number of divs, use getElementsByTa gName("div") on the canvas and check the contents of each one.

        Comment

        • cleary1981
          New Member
          • Jun 2008
          • 178

          #5
          I can get the elements by tagname but I cant get at the one I want. Can anyone advise? Heres what ive been trying

          Code:
          var arr = new Array(); arr = document.getElementsByTagName("div");  //make array of all divs with id = newObject
          	// alert("Total Number of HTML Elements Found: " + document.documentElement.getElementsByTagName("div").length); 
          	for(var i=0; i < arr.length; i++) 
          		{
          			//check innerHTML against obj_name
          			alert("innerHTML" + document.documentElement.getElementsByTagName("*").item(i).nodeName);
          		}

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            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.getElementsB yTagName("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.

            Comment

            • cleary1981
              New Member
              • Jun 2008
              • 178

              #7
              changed the id and was able to do it quite simply. thanks

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

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

                Comment

                Working...