admin管理员组文章数量:1393350
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();
}
}
本文标签: javascriptHow to pass the class name of a clicked div as a parameter to a functionStack Overflow
版权声明:本文标题:javascript - How to pass the class name of a clicked div as a parameter to a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744613904a2615819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论