admin管理员组文章数量:1026971
I have a grid of buttons where user opens up the gird by clicking on the (Open Grid) link and then clicking on a button in that grid. What I want to do is that if user clicks on the "True or False" button, then it should output buttons "True" and "False" underneath the "Number of Answers" textbox. But it is not displaying anything. What am I doing wrong?
The code is here:
$(".gridBtns").click(function() {
var clickedNumber = this.value;
$('.answerBtns').each(function (index) {
if (index < clickedNumber)
$(this).show();
else
$(this).hide();
});
if (document.getElementsByClassName("gridBtns").value == "True or False")
{
document.getElementId("answerTrue").style.display = "block";
}
});
Full code is in jsfiddle, click here
I have a grid of buttons where user opens up the gird by clicking on the (Open Grid) link and then clicking on a button in that grid. What I want to do is that if user clicks on the "True or False" button, then it should output buttons "True" and "False" underneath the "Number of Answers" textbox. But it is not displaying anything. What am I doing wrong?
The code is here:
$(".gridBtns").click(function() {
var clickedNumber = this.value;
$('.answerBtns').each(function (index) {
if (index < clickedNumber)
$(this).show();
else
$(this).hide();
});
if (document.getElementsByClassName("gridBtns").value == "True or False")
{
document.getElementId("answerTrue").style.display = "block";
}
});
Full code is in jsfiddle, click here
Share Improve this question edited Dec 11, 2011 at 17:37 BruceyBandit asked Dec 11, 2011 at 17:17 BruceyBanditBruceyBandit 4,33421 gold badges82 silver badges167 bronze badges 4-
1
FWIW
getElementsByClassName
is not available in IE8 and below... but why don't you use jQuery in that case? – Felix Kling Commented Dec 11, 2011 at 17:22 - you are paring in different statement in different place for same element 1) $(".gridBtns").click 2) document.getElementsByClassName("gridBtns").value i am not sure what is happening – manny Commented Dec 11, 2011 at 17:32
-
Interestingly,
querySelector('.classname')
does work in IE8, so is a better choice thangetElementsByClassName
in that regard. – Niet the Dark Absol Commented Dec 11, 2011 at 17:41 - Thanks everybody attempting to answer my question, greatly appreciate it. I have found an answer so this question is solved – BruceyBandit Commented Dec 11, 2011 at 17:59
4 Answers
Reset to default 2Instead of this:
if (document.getElementsByClassName("gridBtns").value == "True or False")
{
document.getElementId("answerTrue").style.display = "block";
}
Try this:
if (this.id=='btnTrueorFalse') {
$('#answerTrue').show();
$('#answerFalse').show();
}
Besides the 1/many error already mentioned in another post, it looks like the original is just looking for the wrong property in the wrong place.
getElementsByClassName
returns a NodeList (which acts like an array), not a single Element.
The jQuery code to equal your code above would be
if($('.gridBtns').val() == 'True or False'){
$('#answerTrue').show(); //you may also use .css() for it, but show() has the same result
}
I've been seeing this code here for a while now:
If you use jQuery, use it. Doing document.getElementById(..) suddenly is just nonsensical. Just use jQuery selectors everywhere, and methods (f.i. .addClass(..) instead of .className = ..). jQuery is the way you access your DOM, using a mix is just confusing.
Also, I notice most of your problems with this Ui stem from the fact that:
- It is a bad UI (no offense, just think about it)
- You use the UI for storing state. Don't do that. UI should be dependent on state, not the other way around.
I have a grid of buttons where user opens up the gird by clicking on the (Open Grid) link and then clicking on a button in that grid. What I want to do is that if user clicks on the "True or False" button, then it should output buttons "True" and "False" underneath the "Number of Answers" textbox. But it is not displaying anything. What am I doing wrong?
The code is here:
$(".gridBtns").click(function() {
var clickedNumber = this.value;
$('.answerBtns').each(function (index) {
if (index < clickedNumber)
$(this).show();
else
$(this).hide();
});
if (document.getElementsByClassName("gridBtns").value == "True or False")
{
document.getElementId("answerTrue").style.display = "block";
}
});
Full code is in jsfiddle, click here
I have a grid of buttons where user opens up the gird by clicking on the (Open Grid) link and then clicking on a button in that grid. What I want to do is that if user clicks on the "True or False" button, then it should output buttons "True" and "False" underneath the "Number of Answers" textbox. But it is not displaying anything. What am I doing wrong?
The code is here:
$(".gridBtns").click(function() {
var clickedNumber = this.value;
$('.answerBtns').each(function (index) {
if (index < clickedNumber)
$(this).show();
else
$(this).hide();
});
if (document.getElementsByClassName("gridBtns").value == "True or False")
{
document.getElementId("answerTrue").style.display = "block";
}
});
Full code is in jsfiddle, click here
Share Improve this question edited Dec 11, 2011 at 17:37 BruceyBandit asked Dec 11, 2011 at 17:17 BruceyBanditBruceyBandit 4,33421 gold badges82 silver badges167 bronze badges 4-
1
FWIW
getElementsByClassName
is not available in IE8 and below... but why don't you use jQuery in that case? – Felix Kling Commented Dec 11, 2011 at 17:22 - you are paring in different statement in different place for same element 1) $(".gridBtns").click 2) document.getElementsByClassName("gridBtns").value i am not sure what is happening – manny Commented Dec 11, 2011 at 17:32
-
Interestingly,
querySelector('.classname')
does work in IE8, so is a better choice thangetElementsByClassName
in that regard. – Niet the Dark Absol Commented Dec 11, 2011 at 17:41 - Thanks everybody attempting to answer my question, greatly appreciate it. I have found an answer so this question is solved – BruceyBandit Commented Dec 11, 2011 at 17:59
4 Answers
Reset to default 2Instead of this:
if (document.getElementsByClassName("gridBtns").value == "True or False")
{
document.getElementId("answerTrue").style.display = "block";
}
Try this:
if (this.id=='btnTrueorFalse') {
$('#answerTrue').show();
$('#answerFalse').show();
}
Besides the 1/many error already mentioned in another post, it looks like the original is just looking for the wrong property in the wrong place.
getElementsByClassName
returns a NodeList (which acts like an array), not a single Element.
The jQuery code to equal your code above would be
if($('.gridBtns').val() == 'True or False'){
$('#answerTrue').show(); //you may also use .css() for it, but show() has the same result
}
I've been seeing this code here for a while now:
If you use jQuery, use it. Doing document.getElementById(..) suddenly is just nonsensical. Just use jQuery selectors everywhere, and methods (f.i. .addClass(..) instead of .className = ..). jQuery is the way you access your DOM, using a mix is just confusing.
Also, I notice most of your problems with this Ui stem from the fact that:
- It is a bad UI (no offense, just think about it)
- You use the UI for storing state. Don't do that. UI should be dependent on state, not the other way around.
本文标签: javascriptWant to display quotTruequot and quotFalsequot buttonsStack Overflow
版权声明:本文标题:javascript - Want to display "True" and "False" buttons - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745653365a2161449.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论