...

View Full Version : total score from selected drop down values



mareddy
03-11-2012, 07:18 AM
total score from selected drop down values
Hello friends
the following is the code i am using without success


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

Philip M
03-11-2012, 10:40 AM
There are, I fear, several errors. :eek: 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


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:-


<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

webdev1958
03-11-2012, 01:53 PM
I would give the <select>s a class and then use document.getElementsByClassName() to group them. Then loop through them and sum their values.


var selsOA = document.getElementsByClassName('mySels');
var total = 0;

for(i=0; i<selsOA.length; i++){
total += Number(selsOA[i].value);
}

Philip M
03-11-2012, 02:20 PM
I would give the <select>s a class and then use document.getElementsByClassName() to group them. Then loop through them and sum their values.


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. :p
But getElementsByTagName() works perfectly.


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.

webdev1958
03-11-2012, 02:23 PM
Alas, document.getElementsByClassName() is not supported by IE.

Why not get your facts straight before posting :thumbsup::D


<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.

Philip M
03-11-2012, 02:26 PM
Why not get your facts straight before posting :thumbsup::D


<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'

webdev1958
03-11-2012, 02:31 PM
Then there is something wrong with your IE9, or are you just trying to save face?

It's a well publicised fact (http://www.webdeveloper.com/forum/showthread.php?t=237651) (and here as well (http://www.msofficeforums.com/excel/10661-ie9-document-getelementsbyclassname-problem.html)) 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


......document.getElementsByClassName() is not supported by IE........

Philip M
03-11-2012, 02:38 PM
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?

webdev1958
03-11-2012, 02:43 PM
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 :rolleyes:.

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.

webdev1958
03-11-2012, 02:45 PM
.....I doubt whether mareddy could manage to do that.

there are plenty of examples on google.

Philip M
03-11-2012, 02:53 PM
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.

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.

webdev1958
03-11-2012, 11:31 PM
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.

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. :D :thumbsup:

webdev1958
03-12-2012, 12:29 AM
I expect you would, but as document.getElementsByClassName() is not supported by IE you would find that it did not work in that browser. :p
But getElementsByTagName() works perfectly.


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.

Philip M
03-12-2012, 09:38 AM
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.

webdev1958
03-12-2012, 11:55 AM
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.

Philip M
03-12-2012, 12:19 PM
Careful, bullant - your infractions have not expired yet. :p You make me happy because you move steadily closer to the day when the mods blow you out of the water (once again).

webdev1958
03-12-2012, 12:31 PM
Careful, bullant - your infractions have not expired yet. :p You make me happy because you move steadily closer to the day when the mods blow you out of the water (once again).

yeah whatever :thumbsup: :)

You clearly still have me confused with someone else and with the garbage you posted earlier in this thread any credibility you had is now shot.

I don't know when this bullant character was supposedly banned, but I've been here 11 months and made nearly 400 posts.

Clearly, the owners of codingforums either don't agree with you or they are happy for members to come back under a new name. It has to be one or the other, you can't have it both ways after 11 months (and 399 posts) of me being here :)

So clearly, either you are totally wrong or members are allowed to come back under new names :)

VIPStephan
03-12-2012, 01:13 PM
I don't know when this bullant character was supposedly banned, but I've been here 11 months and made nearly 400 posts.

Itís not gonna be many more if you continue posting irrelevant insults.
But one thing related to the issue of getElementsByClassName support: That itís supported in IE 9 doesnít mean that it should be used carelessly in production websites because in real life there are still people browsing with IE 7 and 8 and possibly other browsers where it isnít supported.

webdev1958
03-12-2012, 01:41 PM
That it’s supported in IE 9 doesn’t mean that it should be used carelessly in production websites because in real life there are still people browsing with IE 7 and 8 and possibly other browsers where it isn’t supported.

So why are you repeating an issue which has already come up? The solution for me is extremely simple :D :thumbsup: as I posted earlier :)




Originally Posted by Philip M http://www.codingforums.com/images/buttons/viewpost.gif (http://www.codingforums.com/showthread.php?p=1203036#post1203036)
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 :rolleyes:. The solution is so easy for me.

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.So I will continue to use document.getElementsByClassName() wherever I feel like it without caring what you think because the solution for non supporting browsers is so easy for me :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum