Friday, March 16, 2007

Remove/Delete element from page using JavaScript working in FireFox,IE,Opera

        Few days ago, I was recalling my Java script skill. I was generating Grid using java script script. Instead of using Display property ('none' or 'block') to show or hide the control, I was creating and removing element each time. Creating element is not a big deal. However the script for removing element is different in IE and Firefox.

       In IE, we can remove the element using, however this will not work in Firefox.


Fig -(1) Script to remove element in IE.

      In Firefox, you have to use,

var Node1 = document.getElementById("ParentControlID");

Fig -(2) Script to remove element in Firefox.

  Below is the sample code for the same.

      var Node1 = document.getElementById("Parent"); 
      var len = Node1.childNodes.length;
       for(var i = 0; i < len; i++)
            if(Node1.childNodes[i].id == "Child")

Fig -(3) Script to remove child element in Firefox.

Happy Programming !

1 comment:

WORMSS said...

Hello, Would this not be easier?

elToRemove = document.getElementById('controlID');

My question is, you have made a function to get around .removeNode(true);

I am looking for a function that would do .removeNode(false) in firefox.. do you know of a way/