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 12-29-2003, 10:30 PM   PM User | #1
prizsm
New to the CF scene

 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
prizsm is an unknown quantity at this point
replaceChild in response to user input

I'm stumped as to why this code is not working. Any insights would be welcome.

Thanks
prizsm@uneedspeed.net

<html>
<head>
<title>Spanish Rice</title>
<script language="JavaScript">

var howMany = 1;

function fractionise( nFloat )
{
var nMajor = parseInt( nFloat );
var nMinor = ( nFloat - nMajor );
return ( nMajor + ( nMinor != 0 ? " 1/" + ( 1 / nMinor ) : "" ) );
}

function howmanyservings ()
{
var newServings = document.amounts;
var increase = document.servesform.howmany.value;
var ingOne = fractionise( parseInt( increase ) * parseFloat( newServings.beefbroth.value ) );
var beefBroth = document.getElementById( "beefBroth" );
beefBroth.replaceChild( ingOne, beefBroth.firstChild );

ingTwo = fractionise( parseInt( increase ) * parseFloat( newServings.brownrice.value ) );
var brownRice = document.getElementById( "brownRice" );
brownRice.replaceChild( ingTwo, brownRice.firstChild );
}

</script>
</head>
<body bgcolor=ffcc99>
<form name = "amounts">
<input type="hidden" name="beefbroth" value=".5">
<input type="hidden" name="brownrice" value=".25">
</form>
<table width=70% cellspacing=15>
<tr><td><h2><center>Spanish Rice</center></h2></td></tr>
<tr><td><center><i>serves </i><form name="servesform"> <input type="text" name="howmany" size=3 maxlength=3 value="1"></input></form><center><form name= "doit"><input type= "button" name= "multiply" value= "How many?" onclick= "howmanyservings ();"></input></form></center></td></tr>
</table>
<table cellspacing=15>
<tr><td id= "td1"><span id="beefBroth">1/2</span> cup beef broth &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp</td>
<td id= "td2"><span id="brownRice">1/4</span> cup brown rice</td>
</tr>
</table>
</body>
</html>
prizsm is offline   Reply With Quote
Old 12-29-2003, 11:46 PM   PM User | #2
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
ingTwo isn't a node..it's just a string of text (
Edit: and same with ingOne
). replaceChild() takes two nodes as parameters. The first parameter is the new node, the second is the old node which will be replaced.

Code:
ingTwo = fractionise( parseInt( increase ) * parseFloat( newServings.brownrice.value ) );
var brownRice = document.getElementById( "brownRice" );
brownRice.replaceChild( ingTwo, brownRice.firstChild );
Try this:

Code:
function howmanyservings ()
{
var newServings = document.amounts;
var increase = document.servesform.howmany.value;
var ingOne = fractionise( parseInt( increase ) * parseFloat( newServings.beefbroth.value ) );
var beefBroth = document.getElementById( "beefBroth" );
beefBroth.replaceChild( document.createTextNode(ingOne), beefBroth.firstChild );

ingTwo = fractionise( parseInt( increase ) * parseFloat( newServings.brownrice.value ) );
var brownRice = document.getElementById( "brownRice" );
brownRice.replaceChild( document.createTextNode(ingTwo), brownRice.firstChild );
}
Hope that helps!

Happy coding!
nolachrymose is offline   Reply With Quote
Old 12-30-2003, 12:11 AM   PM User | #3
prizsm
New to the CF scene

 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
prizsm is an unknown quantity at this point
I added the createTextNode and ran the code again, but got an error message at replaceChild invalid argument

Thanks for the input.
prizsm is offline   Reply With Quote
Old 12-30-2003, 01:42 AM   PM User | #4
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
Really? It worked for me (Firebird 0.7, Windows XP). What browser are you using?
nolachrymose is offline   Reply With Quote
Old 12-30-2003, 01:44 AM   PM User | #5
prizsm
New to the CF scene

 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
prizsm is an unknown quantity at this point
IE 6 I believe this browser will support replaceChild!?
prizsm is offline   Reply With Quote
Old 12-30-2003, 01:48 AM   PM User | #6
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
I just tested in IE 6, and oddly enough it didn't work. I tested to see if replaceChild() was supported, and it is. I'm just as confused as you, heh.. . Sorry, but I don't know the reason it's not working.

Happy coding!
nolachrymose is offline   Reply With Quote
Old 12-30-2003, 02:05 AM   PM User | #7
prizsm
New to the CF scene

 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
prizsm is an unknown quantity at this point
I'm sorry too! Thanks for spending the time on this riddle. If you get a sudden brainstorm and come up with a solution, let me know!
prizsm 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 06:33 AM.


Advertisement
Log in to turn off these ads.