Reputation: 201
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 "compareaction" is called to compare "mainnumber" and "buttonnumber" and do a different function called "resultcorrect" or "resultwrong" depending on the comparison.
// comparison code here
function compareaction(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="compareaction(\\number in class name as a parameter)"></div>
Upvotes: 0
Views: 12667
Reputation: 30073
Pass class name to function
<div class="example1" onclick="compareaction(this.className)">
then check it inside function
function compareaction(className) {
// className will be 'example1' in this case
var buttonNumber = className.match(/\d+$/)[0];
if (mainnumber == buttonNumber){
resultcorrect();
} else {
resultwrong();
}
}
Upvotes: 3