Go Back   CodingForums.com > :: Client side development > JavaScript programming

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 08-18-2012, 04:10 AM   PM User | #1
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
Lowercase Output

Hi,

For my JavaScript at http://www.payitforwardfriends.com/converter.html I want it so that when you press calculate the result is all in lower-case. In other words, I want it to write the substance in lower-case in the result but not in the list of options.

How do I do this?

Thanks!
Ilan is offline   Reply With Quote
Old 08-18-2012, 04:17 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
See: http://www.webdeveloper.com/forum/sh...d.php?t=263670
jmrker is offline   Reply With Quote
Old 08-18-2012, 04:24 AM   PM User | #3
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
Thanks for your reply but it doesn't seem to work.
Ilan is offline   Reply With Quote
Old 08-18-2012, 04:36 AM   PM User | #4
low tech
Regular Coder

 
low tech's Avatar
 
Join Date: Dec 2009
Posts: 740
Thanks: 149
Thanked 67 Times in 67 Posts
low tech is on a distinguished road
Erm

Quote:
I changed it but it doesn't seem to work.
Quote:
Thanks for your reply but it doesn't seem to work.
Then you are doing something wrong because it works for me.

LT
__________________
Ask not what can I do for myself, but what can I do for others

"The greatest revenge is to accomplish what others say you cannot do."
~ Unknown
low tech is offline   Reply With Quote
Old 08-18-2012, 06:50 AM   PM User | #5
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
so you're saying that at http://www.payitforwardfriends.com/converter.html it works right now?

the result text is all in lowercase? for me it's showing up in uppercase (the first letters of each substance word)
Ilan is offline   Reply With Quote
Old 08-18-2012, 09:05 AM   PM User | #6
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
What do you mean?
Ilan is offline   Reply With Quote
Old 08-18-2012, 01:59 PM   PM User | #7
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by Ilan View Post
so you're saying that at http://www.payitforwardfriends.com/converter.html it works right now?

the result text is all in lowercase? for me it's showing up in uppercase (the first letters of each substance word)
Show specifically which line in your code you are changing and how.

Are you using the error console?
jmrker is offline   Reply With Quote
Old 08-18-2012, 02:20 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You are setting

Code:
var substancetext = "";
and it is never altered to anything else?! So,


Code:
if (substancetext != "") {
    document.getElementById('substancemessage').innerHTML = substancetext.toLowerCase();
}
will never do anything.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-18-2012, 08:09 PM   PM User | #9
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
Quote:
Originally Posted by AndrewGSW View Post
You are setting

Code:
var substancetext = "";
and it is never altered to anything else?! So,


Code:
if (substancetext != "") {
    document.getElementById('substancemessage').innerHTML = substancetext.toLowerCase();
}
will never do anything.
Then why is there an output at all?
Ilan is offline   Reply With Quote
Old 08-18-2012, 08:11 PM   PM User | #10
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
Quote:
Originally Posted by jmrker View Post
Show specifically which line in your code you are changing and how.

Are you using the error console?
I am not using the error console.

Code:
if (substancetext != ""){
			document.getElementById('substancemessage').innerHTML = substancetext();
		}
was changed to

Code:
if (substancetext != ""){
			document.getElementById('substancemessage').innerHTML = substancetext.toLowerCase();
		}
like you said.

Aren't we dealing with result and not substancetext?

Last edited by Ilan; 08-18-2012 at 08:38 PM..
Ilan is offline   Reply With Quote
Old 08-19-2012, 12:42 AM   PM User | #11
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
You're code is confusing...

If you want 'result' to be the display then you should try...
Code:
  result = result.toLowerCase();
If it is an element called 'result' (a textbox?), then ...
Code:
  document.getElementById('result').value = result.toLowerCase();
Wherever/Whenever your output is to be updated, you will need to use the .toLowerCase() function (if it is changed in several places).
jmrker is offline   Reply With Quote
Old 08-19-2012, 12:57 AM   PM User | #12
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
I'm confused too. Let me try to explain this more clearly:

Alright see the textbox at the bottom that says result? When you select a substance, select a from this and a to this, and press calculate that box is filled.

The substances in the box are capitalized (the first letter of each word). I would like them to be lowercase. Where do I place the necessary code?
Ilan is offline   Reply With Quote
Old 08-19-2012, 01:46 AM   PM User | #13
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Exclamation

Did you write the code, copy it or have someone else do it for you?

What does this mean to do?
Code:
  var result;
  document.getElementById('result').value = result.toLowerCase();
Not what you think, I am sure.
jmrker is offline   Reply With Quote
Old 08-19-2012, 01:58 AM   PM User | #14
Ilan
New Coder

 
Join Date: Aug 2012
Posts: 87
Thanks: 4
Thanked 0 Times in 0 Posts
Ilan is an unknown quantity at this point
I am editing the code someone else wrote and making major changes.

Originally, there was a line of text underneath the "result" that was substancetext. When I got rid of it, for some reason the conversion stopped to work. That's why that line is there. I was told to put the lowercase code there to fix it, and tried it and forgot to remove it. There is no reason why that would work. Substance text is not visible to the user, only result. I will remove it now.
Ilan is offline   Reply With Quote
Old 08-19-2012, 02:23 AM   PM User | #15
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Still rather confusing but start from here for further discussions ...
Code:
<html>
<head>
<title> Undefined </title>
<script type="text/javascript">

function eval_and_fix(formula){
  var input_number;
  input_number = eval(formula) + '';

  // If scientific notation, return
  if ((input_number.indexOf("e") > -1) || (input_number.indexOf("E") > -1)) { return input_number; }

  // if an integer, return
  if (input_number.indexOf('.') == -1) { return input_number; }

  // Seperate and round to 12
  var first, last, split_at;

  split_at = input_number.indexOf('.');

  first = input_number.substr(0,split_at);

  last = input_number.substr(split_at, input_number.length - split_at);
  //last = Math.round(last * 1000000000000);
  //last /= 1000000000000;
  // round to only 3 instead
  last = Math.round(last * 10000);
  last /= 10000;

  return parseInt(first) + last;
}

function parseinputweight2vol(inputamount, inputdensity) {
  var teststring;
  var density = document.getElementById('density').value;
  var quantity = document.getElementById('quantity').value;

  if ( quantity.length == 0 ) {
	alert ("Please enter the amount you want to convert!");
	return false;
  }
  if ( isNaN(parseFloat(quantity)) ) {
	alert ("The entered amount isn't a number!");
	return false;
  }
  if ( parseFloat(quantity) <= 0 ) {
	alert ("The amount have to be positive!");
	return false;
  }
  if ( density.length == 0 ) {
	alert ("Please enter the density or choose the substance from the list!");
	return false;
  }
  if ( isNaN(parseFloat(density)) ) {
	alert ("The entered density isn't a number!");
	return false;
  }
  if ( parseFloat(density) <= 0 ) {
	alert ("The density have to be positive!");
	return false;
  }
  return true;
}

function calcweight2vol() {
  if ( parseinputweight2vol() == false ) { return false; }

  var inunit = document.getElementById('inunit');
  var inunittext = inunit.options[inunit.selectedIndex].text;
  var inunitval = parseFloat(inunit.value);

  var outunit = document.getElementById('outunit');
  var outunittext = outunit.options[outunit.selectedIndex].text;
  var outunitval = parseFloat(outunit.value);

  var densunit = document.getElementById('densunit');
  var densunittext = densunit.options[densunit.selectedIndex].text;
  var densamount = parseFloat(document.getElementById('density').value);

  var substance = document.getElementById("substance");
  var substanceAsText = substance.options[substance.selectedIndex].text;

  var densvalue = densamount / parseFloat(densunit.value);
  var quantity = parseFloat (document.getElementById('quantity').value);
  var amount = quantity;

  var substancetext = "";

  var result;

// first we convert a given amount of substance to grams
  if ( inunitval > 0 ) { amount = amount * inunitval; }
                  else { amount = amount * (-inunitval) * densvalue; }

// then we convert the grams to given output unit

  if ( outunitval > 0 ) { result = amount / outunitval; }
                   else { result = amount / densvalue / (-outunitval); }
  result = eval_and_fix(result);


    var str  = quantity + " " + inunittext + "s of " + substanceAsText + " = " + result + " " + outunittext + "s of " + substanceAsText;
    document.getElementById('result').value = str.toLowerCase();

/*  something goofy about the following logic that caused the display problem
  if (quantity != "1" && result != "1") {
    document.getElementById('result').value
      = quantity + " " + inunittext + "s of " + substanceAsText + " = " + result + " " + outunittext + "s of " + substanceAsText;
  } 
  else {
    if (quantity != "1" && result == "1") {
      document.getElementById('result').value
        = quantity + " " + inunittext + "s of " + substanceAsText + " = " + result + " " + outunittext + " of " + substanceAsText;
    } else {
      if (quantity == "1" && result != "1") {
	document.getElementById('result').value
          = quantity + " " + inunittext + " of " + substanceAsText + " = " + result + " " + outunittext + "s of " + substanceAsText;
      } else {
	document.getElementById('result').value
          = quantity + " " + inunittext + " of " + substanceAsText + " = " + result + " " + outunittext + " of " + substanceAsText;
      }
    }
  }
*/
  if (substancetext != ""){
	document.getElementById('substancemessage').innerHTML = substancetext.toLowerCase();
  }
//  alert(result);
//  document.getElementById('result').value = result.toLowerCase();
}

function copydensity() {
  resetForm();
  if (document.getElementById('substance').selectedIndex == -1) { return; }
  document.getElementById('density').value
    = eval_and_fix(parseFloat(document.getElementById('substance').value) * parseFloat(document.getElementById('densunit').value));
}

function checkcopydensity() {
  if (document.getElementById('substance').selectedIndex == -1) { return; }
  copydensity();
}

function resetdensityForm() {
  document.getElementById('substance').selectedIndex = -1;
  resetForm();
}

function resetForm() {
  document.getElementById('result').value = "";
  document.getElementById('substancemessage').innerHTML = "&nbsp;";
}

</script>
</head>
<body>
<br><center>
<form align="center">
<table>
 <tr>
  <td align="center">Substance</td>
 </tr>
 <tr>
  <td align="center">
   <select id="substance" onchange="copydensity();" size=10>
    <option value="0.508398656">All Purpose Flour</option>
    <option value="0.680251723">Almonds</option>
    <option value="0.817021276">Baking Powder</option>
    <option value="0.508398656">Bread Flour</option>
    <option value="0.766632894">Brown Sugar</option>
    <option value="1.016797312">Butter</option>
    <option value="0.471198754">Cake/Pastry Flour</option>
    <option value="0.64680851">Chocolate Chips</option>
    <option value="0.42931442">Cocoa Powder</option>
    <option value="0.680251723">Corn Meal</option>
    <option value="0.627245095">Corn Starch</option>
    <option value="0.743044189">Dried Currants</option>
    <option value="0.319148936">Flaked Coconut</option>
    <option value="0.902763969">Granulated Sugar</option>
    <option value="0.425531914">Grated Coconut</option>
    <option value="0.851063829">Ground Almonds</option>
    <option value="0.611365472">Hazelnuts</option>
    <option value="1.533265789">Honey</option>
    <option value="0.508398656">Icing Sugar</option>
    <option value="1.446808511">Molasses</option>
    <option value="0.680251723">Peanuts</option>
    <option value="0.480575844">Pecans</option>
    <option value="0.611365472">Pistachios</option>
    <option value="0.508398656">Powdered Sugar</option>
    <option value="0.743044189">Raisins</option>
    <option value="0.407576981">Rolled Oats</option>
    <option value="1.225531915">Salt</option>
    <option value="0.872340425">Shortening</option>
    <option value="0.340425531">Sliced Almonds</option>
    <option value="0.958170212">Sour Cream</option>
    <option value="0.480575844">Walnuts</option>
    <option value="0.542672722">Whole Wheat Flour</option>
   </select>
  </td>
  <td style="display: none;" align="center">
   <INPUT id="density" type="text" value="1" size="12" onFocus="resetdensityForm();"><br>
   <select id="densunit" onchange="checkcopydensity()">
    <option value="240">grams/cup</option>
    <option selected value="1">g/cm³</option>
   </select >
  </td>
 </tr>
 <tr>
  <td colspan="2">Convert what quantity?: <INPUT id="quantity" type="text" value="1" size="7" onFocus="resetForm();"></td>
 </tr>
 <tr>
  <td align="center">From this</td><td align="center">To this</td>
 </tr>
 <tr>
  <td align="center">
   <select id="inunit" size=7 onchange="resetForm();">
    <option value="-240">cup</option>
    <option value="1">gram</option>
    <option value="28">ounce</option>
    <option value="454">pound</option>
    <option value="-15">tablespoon</option>
    <option value="-5">teaspoon</option>
   </select >
  </td>
  <td align="center">
   <select id="outunit" size=7 onchange="resetForm();">
    <option value="-240">cup</option>
    <option value="1">gram</option>
    <option value="28">ounce</option>
    <option value="454">pound</option>
    <option value="-15">tablespoon</option>
    <option value="-5">teaspoon</option>
   </select>
  </td>
 </tr>
 <tr>
  <td colspan="2" align="center">
   <button type="button" OnClick="calcweight2vol()">Calculate</button>
  </td>
 </tr>
 <tr>
  <td colspan="2">Result:<BR>
   <INPUT type="text" size="60" id="result" value="">
  </td>
 </tr>
 <tr>
  <td colspan=2 id="substancemessage">&nbsp;</td>
 </tr>
</table>

</form>
<script type="text/javascript">
 copydensity();
</script>
</body>
</html>
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
font, format, lowercase, text

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 09:23 PM.


Advertisement
Log in to turn off these ads.