Results 1 to 9 of 9

Thread: simple calculation

  1. #1
    Join Date
    Sep 2006
    Location
    My Room
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face simple calculation

    hye experts..
    im a very newbie baby with javascript and need some help.

    Its about a match calculation
    my problem is so simple, there is 2 drop down list
    what im trying to do is, how can i add the selected value of dropdownA and dropdownB
    and then display the value of it


    sorry all..im to naive

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Technically speaking, there is no selected value of a drop down other than that assigned to its selected object (its selected option's value attribute). One can get the text object of the selected option as well. In either case this is a string variable, not a number variable. If it happens to be a number it can be converted to a number variable and then math may be performed on it.

    For anyone to understand what you are trying to do though would require at least an example of a typical situation in which you wish to carry out these operations. Where you want the result displayed would be helpful too. An idea of what this would be used for, even better.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <form action="">
      <select onchange="document.getElementById('mOutput').firstChild.nodeValue = parseFloat(this.value) + parseFloat(this.form.elements['two'].value);" name="one">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
      <select onchange="document.getElementById('mOutput').firstChild.nodeValue = parseFloat(this.value) + parseFloat(this.form.elements['one'].value);" name="two">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
    </form>
    <p id="mOutput"> </p>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Twey, a good basic overview but, it makes certain assumptions about the form that may or may not not apply to what Redsim has in mind and I'm reasonably sure that it will not work in IE. In IE you need the select's this.options[this.selectedIndex].value where you have its this.value. And similarly for the value of the other select when used in the onchange event of one select.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm reasonably sure that it will not work in IE.
    It will.
    Twey, a good basic overview but, it makes certain assumptions about the form that may or may not not apply to what Redsim has in mind
    I wasn't aware that Redsim had a form already built. If so, he certainly didn't mention it.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    From the IE javascript error dialogue:

    'document.getElementById(...).firstChild' is nul or not an object
    I was wrong about the other thing though, that part works.

    Opera doesn't work as expected either.

    Putting this in the head:

    Code:
    <script type="text/javascript">
    onload=function(){
    var tot=document.createTextNode('');
    document.getElementById('mOutput').appendChild(tot);
    }
    </script>
    fixes IE but not Opera. Opera gives no error though, just doesn't work.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    After further testing, this appears to be the only reliable x-browser method:

    Code:
    <form action="">
      <select onchange="document.getElementById('mOutput').innerHTML = parseFloat(this.value) + parseFloat(this.form.elements['two'].value);" name="one">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
      <select onchange="document.getElementById('mOutput').innerHTML = parseFloat(this.value) + parseFloat(this.form.elements['one'].value);" name="two">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
    </form>
    <p id="mOutput"></p>
    And the problem with Opera was that it saw the appended text node not as the first child. However, Opera would work if the empty space was removed from the p element, this threw of FF though. With enough object testing it could probably be made to work with the DOM but, I think that this is a case where innerHTML is appropriate for use with a form, it isn't being used in the form so, it won't overwrite the form's values.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Sep 2006
    Location
    My Room
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    wow..it works fine...
    thanks to prof jscheuer1 and Twey..
    actually i've made some typo error in my 1st post..
    the exact word is 'math' not 'match'...
    sorry to jscheuer1 for making you confusing

  9. #9
    Join Date
    Sep 2006
    Location
    My Room
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    actually my real situation are like this..

    im trying to combine java struts with javascript.
    i have a table generated with <display:column>
    every row of the generated table contain a dropdown

    how can i place arrays for the table rows in the javascript to
    count every dropdown values selected in every row.

    is it possible to combine it? if so please give me some guide as a kickstart

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •