Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2003, 07:01 AM   PM User | #1
Graeme Hackston
Regular Coder

 
Join Date: Jun 2002
Posts: 624
Thanks: 0
Thanked 0 Times in 0 Posts
Graeme Hackston is an unknown quantity at this point
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)
Graeme Hackston is offline   Reply With Quote
Old 01-06-2003, 10:58 AM   PM User | #2
ahosang
Regular Coder

 
Join Date: Sep 2002
Location: Surrey, UK
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
ahosang is an unknown quantity at this point
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)">
ahosang is offline   Reply With Quote
Old 01-06-2003, 05:37 PM   PM User | #3
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
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.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 01-06-2003, 11:20 PM   PM User | #4
Graeme Hackston
Regular Coder

 
Join Date: Jun 2002
Posts: 624
Thanks: 0
Thanked 0 Times in 0 Posts
Graeme Hackston is an unknown quantity at this point
Thanks guys, that makes sense.
Graeme Hackston is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:41 AM.


Advertisement
Log in to turn off these ads.