Solved

Javascript If Then Return Value

Posted on 2013-10-29
6
219 Views
Last Modified: 2013-10-29
I am stuck on what I have a feeling is a pretty simple JS.

I have a button that when clicked does a calculation based on form fields and returns a numeric value.

I would like to add an additional field to evaluate the numeric value and return a description like "Superior" "Good" "Adequate" etc.

How do I write a separate formula to return the description by evaluating the calculated number field something like:  if score less than 75 then Poor else if score between 75 and 80 then Not Adequate so on and so forth.

Thank you in advance for any help on this.
0
Comment
Question by:mounty95
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39609561
Need some context for a proper answer but something like this

function getdescription(score){
     if(score<=100){getdescription="Superior"}
     if(score<=80){getdescription="Good"}
     if(score<=75){getdescription="Adequate"}
     return getdescription
}
0
 

Author Comment

by:mounty95
ID: 39609568
What do your mean context?  

I have a button that the user clicks after filling out a form.  The value is returned in a textbox with id="s".

I want to evaluate the value in the textbox and return in a separate textbox a descriptor for that calculation.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39609571
Context is giving us some code and not just saying I have a button.

Why evaluate the textbox, just work out the description (using code like above) when the button is pressed.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mounty95
ID: 39609669
Here is the formula that returns a value:

function calc(thisform)
{
var rf=thisform.rf.value;
var ap=thisform.ap.value;
var ts=thisform.ts.value;
thisform.s.value=math.round(rf+ap+ts)

How would I (in the same code) evaluate so that the value in s and a description get returned?

Sorry to be so vague.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39609685
function calc(thisform)
{
var rf=thisform.rf.value;
var ap=thisform.ap.value;
var ts=thisform.ts.value;
totalscore=math.round(rf+ap+ts)
thisform.s.value=totalscore

// amend these as necessary
     if(totalscore<=100){description="Superior"}
     if(totalscore<=80){description="Good"}
     if(totalscore<=75){description="Adequate"}
// add a text box to the form and assign it an id and change it below.
document.getElementById("mytextbox").value=description
}
0
 

Author Closing Comment

by:mounty95
ID: 39609714
Thank you for your help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question