Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    total score from selected drop down values

    total score from selected drop down values
    Hello friends
    the following is the code i am using without success

    Code:
    var numQuestn = 6;
    
    function GetScore(form) {
    
        var score = 0;
        var item = 0;
        var currQuestn = 0;
    
        for (i = 0; i < numQuestn; i++) {
    
            item = form.q1.selectedIndex; {
                score += eval(form.q1.options[item].value);
            }
    
            item = form.q2.selectedIndex; {
                score += eval(form.q2.options[item].value)
            }
    
            item = form.q3.selectedIndex; {
                score += eval(form.q3.options[item].value)
            }
    
            item = form.q4.selectedIndex; {
                score += eval(form.q4.options[item].value)
            }
    
            item = form.q5.selectedIndex; {
                score += eval(form.q5.options[item].value)
            }
    
            item = form.q6.selectedIndex; {
                score += eval(form.q6.options[item].value)
            }
    
    
        }
        form.total.value = score
    }​
    i am not able to find the error

    Please help me
    Thank you in advance
    Last edited by VIPStephan; 03-12-2012 at 12:06 PM. Reason: inserted code BB tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    There are, I fear, several errors. Did you try using your error console? You need to revise your understanding of the use of braces {} and square brackets [].

    To capture the value of a select list option, use simply

    val = document.formname.selectlistname.value; // no need for selectedIndex, still less for eval()

    or

    val = document.getElementById("selectlistid").value.

    At its simplest

    Code:
    var score = 0;
    score += Number(document.formname.q1.value);  // the value of the option must be a number
    score += Number(document.formname.q2.value);
    // and so on
    or better using a loop:-

    Code:
    <script type = "text/javascript">
    
    var numQuestion = 6;
    var score = 0;
    
    function getScore() {
    for (var i=1; i<=numQuestion; i++) {
    // Note that if a property name is being generated at run-time, the square bracket notation is required. 
    var x = Number(document.formname["q" + i].value) || 0;  // the value of the option must be a number
    score += x;  // add, not concatenate!
    }
    alert (score);
    }
    
    </script
    Do not use eval()!! The eval() function in Javascript is a way to run arbitrary code at run-time. eval() should rarely, if ever, be used. If it exists in your page, there is almost always a better and more correct way to accomplish what you are doing.

    Please take a look at the forum rules and guidelines regarding double-posting and the use of code tags.

    Quizmaster: Which American Civil War General had the nickname "Stonewall"?
    Contestant: Custard
    Last edited by Philip M; 03-11-2012 at 10:43 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    I would give the <select>s a class and then use document.getElementsByClassName() to group them. Then loop through them and sum their values.

    Code:
    var selsOA = document.getElementsByClassName('mySels');
    var total = 0;
    
    for(i=0; i<selsOA.length; i++){
         total += Number(selsOA[i].value);
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by webdev1958 View Post
    I would give the <select>s a class and then use document.getElementsByClassName() to group them. Then loop through them and sum their values.

    Code:
    var selsOA = document.getElementsByClassName('mySels');
    var total = 0;
    
    for(i=0; i<selsOA.length; i++){
         total += Number(selsOA[i].value);
    }
    I expect you would, but as document.getElementsByClassName() is not supported by IE you would find that it did not work in that browser.
    But getElementsByTagName() works perfectly.

    Code:
    var sels  = document.getElementsByTagName('select');
    var total = 0;
    
    for (i=0; i<sels.length; i++) {
    total += Number(sels[i].value);
    }
    As always, there are several different ways to skin a cat. But some are more effective than others.
    Last edited by Philip M; 03-11-2012 at 01:23 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    Alas, document.getElementsByClassName() is not supported by IE.
    Why not get your facts straight before posting

    Code:
        <body>
            <p class="myClass"></p>
            <p class="myClass"></p>
            <script type="text/javascript">
                var pObjs = document.getElementsByClassName('myClass');
                alert(pObjs.length);  //outputs 2
            </script>
        </body>
    works perfectly well in my IE9.

    You're probably running an old version of IE.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by webdev1958 View Post
    Why not get your facts straight before posting

    Code:
        <body>
            <p class="myClass"></p>
            <p class="myClass"></p>
            <script type="text/javascript">
                var pObjs = document.getElementsByClassName('myClass');
                alert(pObjs.length);  //outputs 2
            </script>
        </body>
    works perfectly well in my IE9.

    You're probably running an old version of IE.
    No, I am running IE9, and it returns
    Error: Object doesn't support property or method 'getElementsByClassName'

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Then there is something wrong with your IE9, or are you just trying to save face?

    It's a well publicised fact (and here as well) that from IE9 document.getElementsByClassName() is supported

    If for some reason it doesn't work in your installation, then just write your own or google one.

    Just because it's not working on your pc, don't go round posting horse manure like

    Quote Originally Posted by Philip M View Post
    ......document.getElementsByClassName() is not supported by IE........
    Last edited by webdev1958; 03-11-2012 at 01:39 PM.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Yes, I thought it was supported as well, but the fact is that my version 9.0.8112.16121 (64 bit edition) returns that error. Sure, I can write my own, but I doubt whether mareddy could manage to do that.

    In any case, why use something that does not work in IE8 which is still widely used?
    Last edited by Philip M; 03-11-2012 at 01:40 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    In any case, why use something that does not work in IE8 which is still widely used?
    That's gotta go down as one of the dumbest questions I have ever beeen asked .

    I have my own getElementsByClassName in my own js library which I link to pages that need it. You then test if the function is supported by the browser. If it is then it's generally better to use the browser supported version. If the browser doesn't support it, then my version is used as a backup.

    There is no extra work involved at all.

  • #10
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    .....I doubt whether mareddy could manage to do that.
    there are plenty of examples on google.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    I would be interested to learn if other people using IE9 have experienced this problem - Error: Object doesn't support property or method 'getElementsByClassName' .
    I agree that it is very odd.

    Edit: The answer is that there must be a <!DOCTYPE html> specified.
    Then it does indeed work as expected.
    Even <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    will do.
    Last edited by Philip M; 03-11-2012 at 03:03 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    Quote Originally Posted by Philip M View Post
    I would be interested to learn if other people using IE9 have experienced this problem - Error: Object doesn't support property or method 'getElementsByClassName' .
    I agree that it is very odd.

    Edit: The answer is that there must be a <!DOCTYPE html> specified.
    Then it does indeed work as expected.
    Even <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    will do.
    Without a doctype IE goes into quirksmode. (That's well publicised as well )

    Have a read of the history behind the need for quirksmode and it should become obvious why having a web page without a valid doctype, like you apparently have done, is a very dumb thing to do.

    [/ot]
    Last edited by webdev1958; 03-11-2012 at 10:42 PM.

  • #13
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    Quote Originally Posted by Philip M View Post
    I expect you would, but as document.getElementsByClassName() is not supported by IE you would find that it did not work in that browser.
    But getElementsByTagName() works perfectly.

    Code:
    var sels  = document.getElementsByTagName('select');
    var total = 0;
    
    for (i=0; i<sels.length; i++) {
    total += Number(sels[i].value);
    }
    Imo that is not a smart way of doing it from a maintenance point of view.

    If in the future another unrelated <select> is added to the page then it would be included in sels which is not what you would want.

    That's why I gave the relevent <select>s a class and used document.getElementsByClassName() (which of course is supported by IE9 and contrary to your statement). Then you can add as many <select>s as you like and they would not be included in sels unless you wanted to give them the appropriate class.



    [/ot]
    Last edited by webdev1958; 03-11-2012 at 11:32 PM.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by webdev1958 View Post
    Without a doctype IE goes into quirksmode. (That's well publicised as well )
    Indeed it does. But quirksmode is supposed to relate to display and css, and has nothing to do with Javascript.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    Quote Originally Posted by Philip M View Post
    But quirksmode is supposed to relate to display and css, and has nothing to do with Javascript.
    But in this case it obviously does according to your solution to getting document.getElementsByClassName() to work. Whether it should or shouldn't is another issue and probably moot imo because no web developer worth their salt should have a web page without a valid doctype.
    [/ot]
    Last edited by VIPStephan; 03-12-2012 at 12:10 PM. Reason: removed flaming


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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