0

I've looked through the prior questions but do not see an answer that I can understand (they are all more complicated than mine).

I'm bootstrapping some javascript using old manuals and my experiences using a scripting language back 15 years ago.

By modifying a tutorial file I have this code and it works fine

var oemdc1 = parseInt(document.getElementById("vehicle_oem_draw").value);        
var oemdc2 = parseInt(document.getElementById("vehicle_added_draw").value);
var oemdc3 = parseInt(document.getElementById("new_vehicle_draw").value);
var oemdc4 = parseInt(document.getElementById("include_prism_draw").value);
var total_current_draw = document.getElementById("total_hourly_current_draw");
total_current_draw.value = oemdc1 + oemdc2 + oemdc3

But I need to add this code so that if the user clicks a radio button (include_prism_draw) they get a different total.

if (oemdc4 == 1)
total_current_draw.value = oemdc1 + oemdc2 + oemdc3 + prism_cd;
else
total_current_draw.value = oemdc1 + oemdc2 + oemdc3;

But I get the added value (prism_cd) in my calculation regardless of the radio button values (a "1" or a "0"). Even if neither button is clicked I still get the added value.

So I think I need some braces or parentheses or something.

I have the var prism_cd declared at the top of the doc and it is inserted into a results field so it is working in that sense.

Any help is much appreciated.

(Okay, found the edit link, they should make it more prominent).

I cut/pasted the code from @Adam and still get the prism_cd regardless of the state of the buttons. (prism_cd is a number I set as a var and it shows up accurately but even when I don't want it.)

the button code is below. Maybe there is a simple mistake

Include PRISM 1.5 mA current draw in calculation?  
<input type="radio" name="include_prism_draw" id="include_prism_draw" value="1" />  Yes
<input type="radio" name="include_prism_draw" id="include_prism_draw" value="0" /> &nbsp;&nbsp;&nbsp;No

To answer the other question about the vars, they are from popups the user manipulates, the script adds the values from the popups and does so accurately until I add the yes/no code with the buttons.

If the user wants to add the prism current draw (prism_cd) they click yes and it is to be added but as I say it is getting added whenever the code is in the script. At this point I do not have either button set to be checked.

The rest of script works accurately as I can test with the spreadsheet I am porting it from.

I still have more things to work through but they are mostly based on this type of "if/else set a var" logic so once I get this working hopefully I should be good to go.

I very much appreciate the replies.

M./

WyattsDad
  • 1
  • 2

2 Answers2

2

I'm not certain what your problem is. But, the best practice for if..else syntax is to put both blocks in braces.

var oemdc1 = parseInt(document.getElementById("vehicle_oem_draw").value);        
var oemdc2 = parseInt(document.getElementById("vehicle_added_draw").value);
var oemdc3 = parseInt(document.getElementById("new_vehicle_draw").value);
var oemdc4 = parseInt(document.getElementById("include_prism_draw").value);
var total_current_draw = document.getElementById("total_hourly_current_draw");
if (oemdc4 === 1){
  total_current_draw.value = oemdc1 + oemdc2 + oemdc3 + prism_cd;
} else {
  total_current_draw.value = oemdc1 + oemdc2 + oemdc3;
}
Adam
  • 43,763
  • 16
  • 104
  • 144
  • I added the code from @Adam and still get the prism_cd regardless of state of the buttons. (prism_cd is a number I set as a var and it shows up accurately but even when I don't want it.) the button code is below. Maybe there is a simple mistake Include PRISM 1.5 mA current draw in calculation?    Yes          No – WyattsDad Jun 11 '16 at 05:54
0

Look at this question: Get Radio Button Value with Javascript

You cannot get the value of a number of associated radio-buttons by just doing

document.getElementById(ID).value;

also look at this question, why you should not give the same id to multiple HTML elements: Why is it a bad thing to have multiple HTML elements with the same id attribute?

Now a possible simple solution for you problem (according to solution from first link):

You could write a function, which returns the value of your two radio-buttons:

function getPrismDrawValue()
{
    // predefined result, if no radio button is checked.
    // in this case result will be 0 -> "No"
    var result = 0;

    // get a list of all HTML-elements with the name 'include_prism_draw'
    var radios = document.getElementsByName('include_prism_draw');
    // loop through all this elements and check if one of them is checked
    for (var i = 0; i < radios.length; i++)
    {
        if (radios[i].checked)
        {
            // get the value of the checked radio button
            result = parseInt(radios[i].value);

            // only one radio can be logically checked, don't check the rest
            break;
        }
    }

    return result;
}

Now your variable oemdc4 should be declared like this:

var oemdc4 = getPrismDrawValue();

EDIT to answer new question:

now your problem is here:

var oemdc4 = parseInt(document.getElementById("prism_draw").value);

if you pass 1.5 to parseInt()-function it will return 1.

use function parseFloat() instead to get your expected result.

var oemdc4 = parseFloat(document.getElementById("prism_draw").value);
Community
  • 1
  • 1
SaschaP
  • 883
  • 1
  • 7
  • 25