CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    First of all, getElementById will, well, get the element by id.

    all of your elements are described by the name property, but they have no id property.

    Secondly, the checkboxes that have the same name will create an array. You would not be able to just grab the value of the array, you would need to add up each value in the array.



    As PeejAvery suggested before, if you are new to javascript then try reading some tutorials. Or maybe pick up a book on javascript .

    You need to gain some basic knowledge of the language yourself so that you can do most of the debugging and troubleshooting on your own.

  2. #17
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    Have 3 books on javascript, only one even helps slightly. I need to see something that applies to my situation so that I can see what I have done wrong.

    I already saw the id mistake and have fixed that.

  3. #18
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    Second my checkboxes all have different names

  4. #19
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    Sorry... don't know how i missed that about the checkboxes.

    Anyways, the id for each element must be different.

    So you shouldn't have 4 radio inputs with id = "General"

    maybe try: id = "General_0"
    id = "General_3" ... etc


    but then, you cannot just have it add all of the id's together, because it will add all the values weather it is checked or not.

    so you need to add conditional statments to verify the which is currently checked.

    Code:
    if (document.getElementById('General_0').checked == true){
    p1 = parseInt(document.getElementById('General_0').value, 10);
    }else if (document.getElementById('General_3').checked == true){
    p1 = parseInt(document.getElementById('General_3').value, 10);
    }else if( .........
    this should work out for you.
    if you are still having trouble, please post your current code again.
    Last edited by Nightwolf629; October 23rd, 2008 at 08:53 AM. Reason: typos

  5. #20
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    Ok, if the radio buttons have different id's they won't act like radio buttons.
    (If one is "selected" the others automatically are "unselected".) So don't I need some kind code inserted that picks the "checked" up? Which is what I thought I was doing.

    Here is the corrected code (without the last fix in place) Since I have totally trashed all the code and started new, I have a calculate button which is supposed to return the total_price (which it doesn't work, and I don't know why).
    This is a demo webpage so you can get an idea of what I am trying to do
    http://www.certainwebdesign.com/fake%20est2.htm

    Code:
    <script type="text/javascript">
    					
    
    function calculate() {
    var p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
    p1 = parseInt(document.getElementsByName('General').value, 10);
    p2 = parseInt(document.getElementsByName('Layout').value, 10);
    p3 = parseInt(document.getElementById('Custom_Graphics').value, 10);
    p4 = parseInt(document.getElementById('Mouse_Over').value, 10);
    p5 = parseInt(document.getElementById('Few_Photos').value, 10);
    p6 = parseInt(document.getElementById('Several_Photos').value, 10);
    p7 = parseInt(document.getElementById('Flash_Menu').value, 10);
    p8 = parseInt(document.getElementById('Flash_Animation').value, 10);
    p9 = parseInt(document.getElementById('Flash_Banner').value, 10);
    p10 = parseInt(document.getElementById('Own_Content').value, 10);
    total_price = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10);
    return  (total_price);
     }
    
    </script>
    <div id="container">
    	<form method="post" action="" name="test">
    		<fieldset name="General">
    		<legend class="alignleft">General</legend>
    		<p>
    		<input name="General" type="radio" checked="checked" value="0" />&nbsp;&nbsp; 
    		I have an existing website.&nbsp; (0 hours)</p>
    		<p>
    		<input name="General" type="radio" value="3"  />&nbsp;&nbsp; 
    		I need a basic blog site.&nbsp; (3 hours)</p>
    		<p>
    		<input name="General" type="radio" value="6"  />&nbsp; 
    		I need a basic, off the shelf, template based site set-up.&nbsp; (6 hours)</p>
    		<p>
    		<input name="General" type="radio" value="15" />&nbsp; 
    		I need a completely custom site.&nbsp; (15 hours)<br />
    		</p>
    		</fieldset> <fieldset name="Layout">
    		<legend class="alignleft">Site Layout</legend>
    		<p>
    		<input name="Layout" type="radio" checked="checked" value="0"  />&nbsp; 
    		I do not need any unique graphics designed.&nbsp;&nbsp; (0 hours)</p>
    		<p>
    		<input name="Layout" type="radio" value="3"  />&nbsp; 
    		I have all my graphics but they need to be integrated. (3 hours)</p>
    		<p>
    		<input name="Layout" type="radio" value="6"  />&nbsp; 
    		I have a template but it needs minor customization.&nbsp; (6 hours)</p>
    		<p>
    		<input name="Layout" type="radio" value="10"  />&nbsp; 
    		I have a template but it needs major customization.&nbsp; (10 hours)</p>
    		<p>
    		<input name="Layout" type="radio" value="20"  />&nbsp; 
    		I would like a custom design created for my site.&nbsp; (20 hours) </p>
    		</fieldset> 
    				
    		<fieldset name="Other Graphics">
    		<legend class="alignleft">Other Graphics</legend>
    		<p>
    		<input id="Custom_Graphics" type="checkbox"  value="4" />&nbsp; 
    		I need some custom graphics made.&nbsp; (4 hours)</p>
    		<p>
    		<input id="Mouse_Over" type="checkbox" value="4" />&nbsp; 
    		I need graphic mouse-over buttons.&nbsp; (4 hours)</p>
    		
    		<p>
    		<input id="Few_Photos" type="checkbox" value="4" />&nbsp;&nbsp; 
    		I need a few stock photographs integrated.&nbsp; (4 hours)</p>
    		
    		<p>
    		<input id="Several_Photos" type="checkbox"  value="9" />&nbsp;&nbsp; 
    		I need several stock photographs integrated.&nbsp; (9 hours)</p>
    		</fieldset> 
    		<fieldset name="Flash">
    		<legend class="alignleft">Flash</legend>
    		<p>
    		<input id="Flash_Menu" type="checkbox"  value="6" />&nbsp; 
    		I need a flash menu.&nbsp; (6 hours)</p>
    		<p>
    		<input id="Flash_Animation" type="checkbox"  value="3" />&nbsp;&nbsp; 
    		I need a short custom flash animation.&nbsp; (3 hours)</p>
    		<p>
    		<input id="Flash_Banner" type="checkbox"  value="4" />&nbsp;&nbsp; 
    		I need a flash banner.&nbsp; (4 hours)</p>
    		</fieldset> 
    		<fieldset name="Content">
    		<legend class="alignleft">Content</legend>
    		<p>
    		<input id="Own_Content" type="checkbox"  value="0" />&nbsp; 
    		I will add my content with a content manager.&nbsp; (0 hours)</p>
    		I will supply
    		<select id="pages" name="pages" >
    		<option value="" selected="selected"></option>
    		<option value="3">1</option>
    		<option value="6">2</option>
    		<option value="9">3</option>
    		<option value="12">4</option>
    		<option value="15">5</option>
    		<option value="18">6</option>
    		<option value="21">7</option>
    		<option value="24">8</option>
    		<option value="27">9</option>
    		<option value="30">10</option>
    		</select> pages of content in MS Word or Notepad text.&nbsp; (3 hours per 
    		page)
    		</fieldset>
    		<div>
    			<input name="Calculate" type="button" value="Calculate" onclick="calculate()" /><input id="total_price" />Total Hours for your site</div>

  6. #21
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    the name property determines the radio groups, not the id

    Code:
      
    <fieldset name="General">
    						<legend class="alignleft">General</legend>
    						<p>
    						<input name="General" id="General_0" type="radio" checked="checked" value="0" />&nbsp;&nbsp;
    						I have an existing website.&nbsp; (0 hours)</p>
    						<p>
    						<input name="General" id="General_3" type="radio" value="3"  />&nbsp;&nbsp;
    						I need a basic blog site.&nbsp; (3 hours)</p>
    						<p>
    						<input name="General" id="General_6" type="radio" value="6"  />&nbsp;
    						I need a basic, off the shelf, template based site set-up.&nbsp; (6 hours)</p>
    						<p>
    						<input name="General" id="General_15" type="radio" value="15" />&nbsp;
    						I need a completely custom site.&nbsp; (15 hours)<br />
    						</p>
    						</fieldset>
    try that along with the conditional statements I posted before.

  7. #22
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    as for the calculate button,

    replace "return (total_price);" with this:
    Code:
      
    document.getElementByID('total_price').value = total_price;

  8. #23
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    The code you gave is working except for the fact that with no buttons activated except "0" values, I get a total of 34. I have looked several times in the code to see what is returning a value and see nothing.

    Any ideas. Is there a way to make the script run 1 step at a time to see where it is coming from.

    The updated page is here:

    http://www.certainwebdesign.com/fake%20est2.htm

    Code:
    <script type="text/javascript">
    					
    
    function calculate() {
    var p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
    
    if (document.getElementById('g0').checked==true) 
    {p1 = parseInt(document.getElementById('g0').value, 10);
    }
    
    else if (document.getElementById('g3').checked == true)
    {p1 = parseInt(document.getElementById('g3').value, 10);
    }
    
    else if (document.getElementById('g6').checked == true)
    {p1 = parseInt(document.getElementById('g6').value, 10);
    }
    
    else if (document.getElementById('g15').checked == true)
    {p1 = parseInt(document.getElementById('g15').value, 10);
    }
    
    if (document.getElementById('L0').checked == true) 
    {p2 = parseInt(document.getElementById('L0').value, 10);
    }
    
    else if (document.getElementById('L1').checked == true)
    {p2 = parseInt(document.getElementById('L1').value, 10);
    }
    
    else if (document.getElementById('L6').checked == true)
    {p2 = parseInt(document.getElementById('L6').value, 10);
    }
    
    else if (document.getElementById('L10').checked == true)
    {p2 = parseInt(document.getElementById('L10').value, 10);
    }
    
    else if (document.getElementById('L20').checked == true)
    {p2 = parseInt(document.getElementById('L20').value, 10);
    }
    
    p3 = parseInt(document.getElementById('Custom_Graphics').value, 10);
    p4 = parseInt(document.getElementById('Mouse_Over').value, 10);
    p5 = parseInt(document.getElementById('Few_Photos').value, 10);
    p6 = parseInt(document.getElementById('Several_Photos').value, 10);
    p7 = parseInt(document.getElementById('Flash_Menu').value, 10);
    p8 = parseInt(document.getElementById('Flash_Animation').value, 10);
    p9 = parseInt(document.getElementById('Flash_Banner').value, 10);
    p10 = parseInt(document.getElementById('Own_Content').value, 10);
    total_price = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10); parseInt(document.getElementById('total_price').value = total_price);
     }
    
    </script>
    <div id="container">
    	<form method="post" action="" name="test">
    		<fieldset name="General">
    		<legend class="alignleft">General</legend>
    		<p>
    		<input name="General" id ="g0" type="radio" checked="checked" value="0" />&nbsp;&nbsp; 
    		I have an existing website.&nbsp; (0 hours)</p>
    		<p>
    		<input name="General" id="g3" type="radio" value="3"  />&nbsp;&nbsp; I need a 
    		basic blog site.&nbsp; (3 hours)</p>
    		<p>
    		<input name="General" id="g6" type="radio" value="6"  />&nbsp; I need a basic, 
    		off the shelf, template based site set-up.&nbsp; (6 hours)</p>
    		<p>
    		<input name="General" id="g15" type="radio" value="15" />&nbsp; I need a 
    		completely custom site.&nbsp; (15 hours)<br />
    		</p>
    		</fieldset> <fieldset name="Layout">
    		<legend class="alignleft">Site Layout</legend>
    		<p>
    		<input name="Layout" id ="L0"type="radio" checked="checked" value="0"  />&nbsp; 
    		I do not need any unique graphics designed.&nbsp;&nbsp; (0 hours)</p>
    		<p>
    		<input name="Layout" id ="L1"type="radio" value="3"  />&nbsp; I have all my 
    		graphics but they need to be integrated. (3 hours)</p>
    		<p>
    		<input name="Layout" id ="L6"type="radio" value="6"  />&nbsp; I have a template 
    		but it needs minor customization.&nbsp; (6 hours)</p>
    		<p>
    		<input name="Layout" id ="L10"type="radio" value="10"  />&nbsp; I have a template 
    		but it needs major customization.&nbsp; (10 hours)</p>
    		<p>
    		<input name="Layout" id ="L20"type="radio" value="20"  />&nbsp; I would like a 
    		custom design created for my site.&nbsp; (20 hours) </p>
    		</fieldset> 
    				
    		<fieldset name="Other Graphics">
    		<legend class="alignleft">Other Graphics</legend>
    		<p>
    		<input id="Custom_Graphics" type="checkbox"  value="4" />&nbsp; I need 
    		some custom graphics made.&nbsp; (4 hours)</p>
    		<p>
    		<input id="Mouse_Over" type="checkbox" value="4" />&nbsp; I need graphic 
    		mouse-over buttons.&nbsp; (4 hours)</p>
    		
    		<p>
    		<input id="Few_Photos" type="checkbox" value="4" />&nbsp;&nbsp; I need a 
    		few stock photographs integrated.&nbsp; (4 hours)</p>
    		
    		<p>
    		<input id="Several_Photos" type="checkbox"  value="9" />&nbsp;&nbsp; I 
    		need several stock photographs integrated.&nbsp; (9 hours)</p>
    		</fieldset>
    		<div>
    			 
    		<fieldset name="Flash">
    		<legend class="alignleft">Flash</legend>
    		<p>
    		<input id="Flash_Menu" type="checkbox"  value="6" />&nbsp; I need a 
    		flash menu.&nbsp; (6 hours)</p>
    		<p>
    		<input id="Flash_Animation" type="checkbox"  value="3" />&nbsp;&nbsp; I 
    		need a short custom flash animation.&nbsp; (3 hours)</p>
    		<p>
    		<input id="Flash_Banner" type="checkbox"  value="4" />&nbsp;&nbsp; I 
    		need a flash banner.&nbsp; (4 hours)</p>
    		</fieldset> 
    		<fieldset name="Content">
    		<legend class="alignleft">Content</legend>
    		<p>
    		<input id="Own_Content" type="checkbox"  value="0" />&nbsp; I will add 
    		my content with a content manager.&nbsp; (0 hours)</p>
    		I will supply
    		<select id="pages" name="pages" >
    		<option value="" selected ="selected"></option>
    		<option value="3">1</option>
    		<option value="6">2</option>
    		<option value="9">3</option>
    		<option value="12">4</option>
    		<option value="15">5</option>
    		<option value="18">6</option>
    		<option value="21">7</option>
    		<option value="24">8</option>
    		<option value="27">9</option>
    		<option value="30">10</option>
    		</select> pages of content in MS Word or Notepad text.&nbsp; (3 hours per 
    		page)
    		</fieldset>
    		<div>
    			<input name="Calculate" type="button" value="Calculate" onclick="calculate()" /><input id="total_price" />Total 
    			Hours for your site</div>
    
    			
    							
    							
    
    					</form>
    					</div>

  9. #24
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    Come on now... I would have figured you would get this part on your own. Its pretty much the same problem you just fixed.

    Add up the values of all your checkboxes, and you get 34.

    You need conditional statments for the checkboxes. Which means you also need id's for them as well.

    Your code is adding the values of all the checkboxes without seeing if they are checked or not.


    Also, it looks like you need to add some code in to calculate the pages dropdown.

    replace this at the end of your javascript:
    Code:
      
    p11 = parseInt(document.getElementById('pages').value, 10);
    total_price = (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11);
    document.getElementById('total_price').value = parseInt(total_price);
    and change the first option of the dropdown to:

    Code:
      
    <option value="0">0</option>

  10. #25
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    Ok, I tried the conditional before I messaged:

    Code:
    if (document.getElementById('Custom_Graphics').checked == true) 
    {p3 = parseInt(document.getElementById('Custom_Graphics').value, 10);
    }
    and returned NaN.
    The 2 radio buttons worked on calculate until I added the p3 statement which is when I get the NaN.

    This was why I asked the last question in the last message.


    Code:
    <input id="Custom_Graphics" type="checkbox"  value="4" />&nbsp; I need 
    		some custom graphics made.&nbsp; (4 hours)</p>
    		<p>
    		<input id="Mouse_Over" type="checkbox" value="4" />&nbsp; I need graphic 
    		mouse-over buttons.&nbsp; (4 hours)</p>
    		
    		<p>
    		<input id="Few_Photos" type="checkbox" value="4" />&nbsp;&nbsp; I need a 
    		few stock photographs integrated.&nbsp; (4 hours)</p>
    		
    		<p>
    		<input id="Several_Photos" type="checkbox"  value="9" />&nbsp;&nbsp; I 
    		need several stock photographs integrated.&nbsp; (9 hours)</p>

  11. #26
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    OK, this works. Don't know if its good javascript or not. Please comment, if not. Let me know what's not kosher.

    Thanks big time for your help! I really appreciate you taking the time. Believe it or not I was really trying to understand what I am doing.

    Code:
    if (document.getElementById('Custom_Graphics').checked == true) 
    {p3 = parseInt(document.getElementById('Custom_Graphics').value, 10);
    }
    else if (document.getElementById('Custom_Graphics').checked == false)
    {p3 = parseInt(0)}

  12. #27
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    That will work, but there is a simpler way.

    The problem is that if the checkbox does not get marked, the p# does not get a value.

    The easy fix is to declare p3-p10 as =0 in the begining of the javascript. That way if nothing gets checked, it is still 0.

    Code:
      
    function calculate() {
     var p3=0, p4=0, p5=0, p6=0, p7=0, p8=0, p9=0, p10=0;

  13. #28
    Join Date
    Oct 2008
    Posts
    15

    Re: uncheck box and subtract/add value

    And not use the "else if" statement?

  14. #29
    Join Date
    Oct 2008
    Location
    Richmond, VA
    Posts
    24

    Re: uncheck box and subtract/add value

    correct,

    the "else if" that is used to set the variable to 0 would no longer be needed

  15. #30
    Join Date
    Oct 2008
    Posts
    15

    Smile Re: uncheck box and subtract/add value

    Thank you again for all your help. This is the first time I have understood what someone was talking about when it comes to "radio button, checkboxes, and option button" javascript and how to get the values. The final script that I have got is simple to understand, without a bunch of mumbo jumbo. And you explained it very well. I am glad to have made your acquaintance.
    Last edited by tcertain; October 23rd, 2008 at 02:58 PM. Reason: spelling

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured