72442

javascript if statement syntax (need help)

Question:

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./

Answer1:

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; }

Answer2:

Look at this question: <a href="https://stackoverflow.com/questions/9618504/get-radio-button-value-with-javascript" rel="nofollow">Get Radio Button Value with Javascript</a>

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: <a href="https://stackoverflow.com/questions/7505350/why-is-it-a-bad-thing-to-have-multiple-html-elements-with-the-same-id-attribute" rel="nofollow">Why is it a bad thing to have multiple HTML elements with the same id attribute?</a>

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

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(); <hr />

<strong>EDIT to answer new question:</strong>

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);

Recommend

  • Combine/Merge Multiple Lines into One Line from a Text File (Powershell)
  • Boost split not traversing inside of parenthesis or braces
  • How to write a bison grammer for WDI?
  • Merge strategy for whitespace only changes?
  • Can you alter the output of %caller{0} in logback to mimic log4j %l specifier?
  • Macro to call a function
  • Anonymous functions and Maps in Scala
  • Regex failing to match number and dash with letter (or space and letter)
  • Does sql server minds the way records where inserted?
  • Receive custom intent without activity restart
  • NHibernate proxyexception
  • Linux command line : edit hacked index files
  • Can't compile Arduino CapSense example
  • change color of jstree node
  • can variables be set randomly when declaring them again?
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Selenium to click on a javascript button corresponding to a text
  • Is playing sound in Javascript performance heavy?
  • Detect when Facebook like button is clicked
  • How to attach a node.js readable stream to a Sendgrid email?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Android fill_parent issue
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • Insert into database using onclick function
  • Can Jackson SerializationFeature be overridden per field or class?
  • Knitr HTML Loop - Some HTML output, some R output
  • JSON with duplicate key names losing information when parsed
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Convert array of 8 bytes to signed long in C++
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Android Studio and gradle
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • How to Embed XSL into XML