Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Javascript, option-select w calc
Old 01-14-2010, 04:52 PM Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
On a normal basis I do not work with javascript at all, I got this js-calc-task to do, with the words, ”you figure it out!”, which left me feeling as an absolute zero from the get go. The task is a special (laaarge) calculator, and compared to my knowledge of js when I started out, I have managed to write aprox 70% of it, so now I feel less of a zero (but yet, still a zero tho ;-).

I have been staring blind for 12 days on my option problem:

This is what it need to do:
User select between (input_g):
Option 50%
Option 100%
Option 150%

After user select an option
each option need to calculate three numbers; fx Option 50% will be “50/100*c”. For 100% itll be “100/100*c” and the last one “150/100*c”.

The end result should be shown in a span-tag <span id="input_g"></span>
--

I have written
function dropdown_value() {
select_element = document.getElementById('input_g');
input_g = select_element.options[select_element.selectedIndex].value;
alert(input_g);
}

And the variable being:
var input_g = input_g/100*c;

The option code looks like this:
<select id="input_g" onchange="dropdown_value();">
<option value="50">50</option>
<option value="100">100</option>
<option value="150">150</option>
</select>

But nothing comes out (not even a zero ;-).. Does anyone see what the problem is?
Chansen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-14-2010, 05:17 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
1) You don't define "c" anywhere.
2) You use 2 id's of the same value.

I changed it a bit and this works:
HTML Code:
<html>
  <head>
  <script>
  var c = 123;
  function dropdown_value() {
    select_element = document.getElementById('input_g');
    input_g = parseInt(select_element.options[select_element.selectedIndex].value);
    input_g = input_g/100*c
    document.getElementById('input_g_calculated').innerHTML = input_g;
  }
  </script>
  </head>
  <body>
  <select id="input_g" onchange="dropdown_value();">
    <option value="0"> &mdash; CHOOSE ONE &mdash; </option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
  </select>
  <span id="input_g_calculated"></span>
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 06:01 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
Thank you for answering :-)

C is user defined earlier on:
c = document.getElementById("input_c").value;
if (isNaN(c)) {
errors.push("C is not a number!<br />");
} else {
c = parseFloat(c);
}

Your version alone or in my code gives the same yellow annoying error and no answer.

IE8 here.
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 06:03 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
A couple of typos:

HTML Code:
<html>
  <head>
  <script>
  var c = 123;
  function dropdown_value() {
    var select_element = document.getElementById('input_g');
    var input_g = parseInt(select_element.options[select_element.selectedIndex].value);
    input_g = input_g/100*c;
    document.getElementById('input_g_calculated').innerHTML = input_g;
  }
  </script>
  </head>
  <body>
  <select id="input_g" onchange="dropdown_value();">
    <option value="0"> &mdash; CHOOSE ONE &mdash; </option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
  </select>
  <span id="input_g_calculated"></span>
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 06:09 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
So I have checked in Firefox, and yours work there, but not mine... so something is fundamentally wrong with mine

As a workaround.. I was wondering if IE work better with radio buttons, what are your thoughts on that?
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 06:11 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
It shouldn't matter which browser you're using. I've tested this in both IE 8 and FF 3.5 and it worked (the last code, not the first).
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 06:15 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
Ups, you are fast. Now your code works fine in IE8. But mine... nope. Weird.
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 06:26 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Then you'll need to give additional information... all code works, just not necessarily as intended.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 06:33 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
Your code is the way it should work. Everything in my calculator works, Firebug having no trouble with my code (thats weird), the only thing that doesnt work is the option thing.

I can choose any of the 3 options, but no answer.
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 06:36 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Hit tab after changing the value to trigger the onChange event. Might want to have a button to activate that.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 06:48 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
I was wondering in the same area. My code is programmed with a timer on user entries earlier on. It has of course timed out a long time ago. Hmm...
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 06:48 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I'm confused.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 06:55 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
I understand.

I check if all user input fields are filled in and after the last entry (6 if them) there is a timer set on 800 ms. What I dont do - I dont check on the option input field (the new 7th) - Maybe this is the core problem here.
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 06:57 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Sounds like you're on the issue, then. If you're stuck after trying a solution, then post the code here.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 07:05 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
HTML Code:
<script>
var t; // variabel to save timeout event
function pre_beregn() {
    input_fields = document.getElementsByName("input_felt");
    empty = 0;
    for (var i=0; i<input_fields.length; i++) { // are all fields filled in?
        if (input_fields[i].value == "") empty = 1;
    }
    if (empty == 0) { // all fields are filled
        t = setTimeout("beregn()",800); // run calc
    }
</script>
I tried to shuffle "select_element = document.getElementById('input_g');" in there after "input_fields=....", but no luck

Last edited by JeremyMiller; 01-14-2010 at 07:11 PM.. Reason: Added code tags for my own sanity. :)
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 07:14 PM Re: Javascript, option-select w calc
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Not sure that that's a very good method given how it worked for me, but here's the modified code:

HTML Code:
<html>
  <head>
  <script>
  var c = 123;
  function dropdown_value() {
    var select_element = document.getElementById('input_g');
    var input_g = parseInt(select_element.options[select_element.selectedIndex].value);
    input_g = input_g/100*c;
    document.getElementById('input_g_calculated').innerHTML = input_g;
  }
var t; // variabel to save timeout event
function pre_beregn() {
  input_fields = document.getElementsByName("input_felt");
  empty = 0;
  for (var i=0; i<input_fields.length; i++) { // are all fields filled in?
      if (input_fields[i].value == "") empty = 1;
  }
  if (empty == 0 && document.getElementById('input_g').options.selectedIndex == 0) { // all fields are filled
      t = setTimeout("beregn()",800); // run calc
  }
}
t = setTimeout("beregn()",800);
  </script>
  </head>
  <body>
  <select id="input_g" onchange="dropdown_value();">
    <option value="0"> &mdash; CHOOSE ONE &mdash; </option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
  </select>
  <span id="input_g_calculated"></span>
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-14-2010, 07:47 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
This is nasty. If we change this timer event it will conflict with shown calculations in between the user input field 6 (which my timeout code handles) and the new user input option 7. It wont work. Hmm... And even if I run true the calculator, filling out the 6 fields, selecting an option.. it still doesnt work. Now I am really lost.
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 07:52 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
In other words, lotsa NaN's
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 08:07 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
Dont respond, I am working on another option to solve this problem. Ill return.
Chansen is offline
Reply With Quote
View Public Profile
 
Old 01-14-2010, 08:47 PM Re: Javascript, option-select w calc
Experienced Talker

Posts: 42
Name: Chansen
Trades: 0
Tried solutions that didnt work
1) If I set the select option in the beginning of the document (Line 14 to Line 00), forcing users to select before the timer-events input fields (Line 6), no calculations comes out (Line 7 to 13) and no answer is given from the option selection (Line 15). This with your new TimeOut code.
2) If I keep the old TimeOut code, and set the selection at the beginning of the document (Line 14 to Line 00), again to force a selection before the timeOut event kicks in (Line 6), I get no answer from the option selection (Line 15). But I do get my calculations shown (Line 7 to 13).

This is how it looks on the html page, (with text after the line numbers...)
01 a // user input
02 b // user input
03 c // user input
04 d // user input
05 e // user input
06 f // user input - timeout event runs
....
07 total = a*b*c*d*e*f //result shown - these are in between calcs shown to user after run of timeout event
08 totalb = a*b*c // result shown
09 totalc = totalb/d // result shown
10 totald = total/45 // result shown
11 totale = totald/a // result shown
12 copy from a
13 copy from b
....
14 Select option <---- Pirate AAaaRRRrgggghh!
15 input_g_calculated // result shown <---- Pirate AAaaRRRrgggghh!
16 copy from d
[etc.]

Any good ideas? Or maybe this is getting pretty difficult to understand... Maybe I should clean up the page, and show the whole thing..
Chansen is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript, option-select w calc

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.06112 seconds with 12 queries