Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    624
    Thanks
    0
    Thanked 0 Times in 0 Posts

    try - catch and removeChild questions

    I have 2 questons regarding the example here:

    http://msdn.microsoft.com/workshop/a...emovechild.asp

    This is the code:

    <html>
    <HEAD>
    <SCRIPT>
    function removeElement()
    {
    try
    {
    //The first child of the div is the bold element.
    var oChild=Div1.children(0);
    Div1.removeChild(oChild);
    }
    catch(x)
    {
    alert("You have already removed the bold element.
    Page will be refreshed when you click OK.")
    document.location.reload();
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <DIV ID=Div1 onclick="removeElement()">
    Click anywhere in this sentence to remove this <B>Bold</B> word.
    </DIV>
    </BODY>
    </html>


    1/ Where does the "x" come from in "catch(x)" ?

    2/ When removing the element, how do I make a variable of the division id ?

    example:

    a = '1'

    document.getElementById('Div' + a)

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function removeElement(a)
    {
    try
    {
    //The first child of the div is the bold element.
    var theDiv=document.getElementById("Div"+a);
    var oChild=theDiv.children(0);
    theDiv.removeChild(oChild);
    }
    catch(x) { //x is just the error object, some people use e - doesn't matter
    alert("You have already removed the bold element.
    Page will be refreshed when you click OK.")
    document.location.reload();
    }
    }

    <DIV ID=Div1 onclick="removeElement(1)">

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    You should be using childNodes.item(N) instead of .children(N)

    I was looking for a link on the JS1.5 guide to explain explain the Error() contructor for you - but it doesn't cover it! Very surprising.

    Anyway, try...catch:
    http://devedge.netscape.com/library/...t.html#1051663

    But unbeknownst to many, on top of your typical Number, String, Boolean, etc constructors, there is an Error one.

    Alex Vincent could tell you all you wanted to know about it... he even got a stacktrace property added to Mozilla's Error.prototype, which is great for debugging.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    624
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys, that makes sense.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •