最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Want to display "True" and "False" buttons - Stack Overflow

matteradmin5PV0评论

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 than getElementsByClassName 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
Add a ment  | 

4 Answers 4

Reset to default 2

Instead 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.
Post a comment

comment list (0)

  1. No comments so far