This may be basic but I could not find the result I needed in JAVASCRIPT from the research I did.
there are two number variables. they are "mainnumber" and "buttonnumber" mainnumber is generated from math.random. buttonnumber is the number that is at the end of the different class names. That means buttonnumber = 1 when the clicked div has the class name class='example1' That means buttonnumber = 2 when the clicked div has the class name class='example2' etc.
there are about 50 divs that have their own class name as example1,example2 etc. some class names are repeated among divs.
when a button is clicked a function "pareaction" is called to pare "mainnumber" and "buttonnumber" and do a different function called "resultcorrect" or "resultwrong" depending on the parison.
// parison code here
function pareaction(buttonnumber){
if (mainnumber == buttonnumber){
resultcorrect();
} else {
resultwrong();
}
}
I need to know how to pass the number at the end of class name (that is 1 or 2 or 3 or 4 etc) to the function.
<div class="example1" onclick="pareaction(\\number in class name as a parameter)"></div>
This may be basic but I could not find the result I needed in JAVASCRIPT from the research I did.
there are two number variables. they are "mainnumber" and "buttonnumber" mainnumber is generated from math.random. buttonnumber is the number that is at the end of the different class names. That means buttonnumber = 1 when the clicked div has the class name class='example1' That means buttonnumber = 2 when the clicked div has the class name class='example2' etc.
there are about 50 divs that have their own class name as example1,example2 etc. some class names are repeated among divs.
when a button is clicked a function "pareaction" is called to pare "mainnumber" and "buttonnumber" and do a different function called "resultcorrect" or "resultwrong" depending on the parison.
// parison code here
function pareaction(buttonnumber){
if (mainnumber == buttonnumber){
resultcorrect();
} else {
resultwrong();
}
}
I need to know how to pass the number at the end of class name (that is 1 or 2 or 3 or 4 etc) to the function.
<div class="example1" onclick="pareaction(\\number in class name as a parameter)"></div>
Share
Improve this question
edited Sep 14, 2014 at 15:28
Anatoliy
30.1k5 gold badges47 silver badges47 bronze badges
asked Sep 14, 2014 at 15:21
nimala9nimala9
2011 gold badge2 silver badges6 bronze badges
1
-
1
Are you sure there will be only 1 class on the element ever? if not, it's better to use a custom
data-
attribute than a classname. – T J Commented Sep 14, 2014 at 16:13
1 Answer
Reset to default 3Pass class name to function
<div class="example1" onclick="pareaction(this.className)">
then check it inside function
function pareaction(className) {
// className will be 'example1' in this case
var buttonNumber = className.match(/\d+$/)[0];
if (mainnumber == buttonNumber){
resultcorrect();
} else {
resultwrong();
}
}