views:

52

answers:

1

kindly check the CODE**

<html>
<head>
<title>DOM ADD?REMOVE UL-LI..... NODE DEMO 09/08/2010 </title>
<style type="text/css">

ul{margin: 0;
padding: 0;
left: 20px;
width: 90%;
border-bottom: solid 1px #d09800;
}
li{ 
color: #333 !important;
background: white;
text-decoration: none;
}
#menu li{
        height:15px;width:90px;
        cursor:pointer;
        border:1px solid #95aea5;
        background:#EFF8FD;
        padding-bottom:6px;
        margin-top:2px;
        text-align:center;
        list-style:none;

}
</style>
<script type="text/javascript">

var TDCount = 3;
var i=0;
function insertTD(){
        var possition=document.getElementById('elmnt_pos').value;
        if(possition=="")
        {
                    possition='a';
                    alert('Enter a number!!!');
         }
        if(isNaN(possition))
        {
          alert('Enter a number!!!');
          document.getElementById('elmnt_pos').value='';
        }else{
 var newTD = document.createElement("li");
        var newid='li'+TDCount++;
           newTD.setAttribute("id", newid);
           newTD.setAttribute("onclick","javascript:removenode(this);" );   
 var newText = document.createTextNode(i+"New fruit " + (possition++));
 newTD.appendChild(newText);

 var trElm = document.getElementById("menu");
 var refTD = trElm.getElementsByTagName("li").item(possition-2);
 trElm.insertBefore(newTD,refTD);
        i++;
        }

}
function removenode(th)
{
answer = confirm("Do you really want to Remove Element "+th.id + " ? ")
        if (answer !=0)
        {
        document.getElementById('menu').removeChild(document.getElementById(th.id));
        }

}
</script>
  </head>
<body>
   <ul id="menu">
    <li id="li0" onclick="javascript:removenode(this);">apple</li>
    <li id="li1" onclick="javascript:removenode(this);">Banana</li>
    <li id="li2" onclick="javascript:removenode(this);">Jackfruit</li>
    </ul>
   <form name="justfrm">
    <input type="text" value="Enter the position" name="pos1" id="elmnt_pos" />
   <input type="button" value="click" onclick="javascript:insertTD()"/>

   </form>
</body>
</html>

"Enter the position" means add element on a specific position like 2,3,5 etc. We can Delete Item by click on item . My problem is that the DELETE ITEM (Item which has been added dynamically) is not deleteing by click in IE6. Kindly any one help me about this DOM matter?

A: 

I don't have an instance on Internet Explorer 6 to test with, but more than likely it's this line, which is causing the problem:

newTD.setAttribute("onclick","javascript:removenode(this);" );

It does not work in Internet Explorer 6. You will need to do something like:

newTD.onclick = function() { removeNode(this); };

or

newTD.onclick = new Function("removenode(this)");

See this article for more information. Also, as a side note you may want to look into using a library like jQuery, which already handles these types of cross-browser issues.

Garett
Thank you so much for you information .I just replace the newTD.setAttribute("onclick","javascript:removenode(this);" );BYnewTD.onclick = new Function("removenode(this)");and it's working perfect ...Thanks a lot.
JAMES
If this answers your question then please accept it as an answer. See http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work
Garett