Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking id to set value

I'm doing a jeopardy board, and I've given each div (each question square) an id in the index.html so when it's clicked, the correct question appears. I also wanted to set the point value for each div based on its id, so I used the jquery .is and this if/else statement. I've only done the code for A1 and A2, but while A2 should be worth $200, it's coming back worth $100. Why?

if ($('.well').is('#A1', '#B1', '#C1', '#D1', '#E1')) {
questionValue = 100;
}
else if ($('.well').is('#A2', '#B2', '#C2', '#D2', '#E2')) {
questionValue = 200;
}
else if ($('.well').is('#A3', '#B3', '#C3', '#D3', '#E3')) {
questionValue = 300;
}
else if ($('.well').is('#A4', '#B4', '#C4', '#D4', '#E4')) {
questionValue = 400;
}
else if ($('.well').is('#A5', '#B5', '#C5', '#D5', '#E5')) {
questionValue = 500;
}

Thank you in advance. Please let me know if I should include any more code. .well is the div class for the question squares.

Adding html code

<div id="wrapper">

     <div class="row">
       <div class="title"><h2></h2></div>
        <div class="question">
        <form> Your Answer:
      <input type="text" id="answer" /></form>
      <button id="submit">Submit</button>
      <p id="instruct">When you're sure about your probably wrong answer, click submit</p>

     <!--  <div class = "result"> </div> -->

    </div>
    </div>


    <div class="row">
       <div class="well" id="A1">  <p>$100</p>  </div>
       <div class="well" id="B1">  <p>$100</p> </div>
       <div class="well" id="C1">  <p>$100</p>  </div>
       <div class="well" id="D1">  <p>$100</p>  </div>
       <div class="well" id="E1">  <p>$100</p>   </div>  
    </div>

    <div class="row">
       <div class="well" id="A2"> <p>$200</p>    </div>
       <div class="well" id="B2"> <p>$200</p>    </div>
       <div class="well" id="C2"> <p>$200</p>    </div>
       <div class="well" id="D2"> <p>$200</p>    </div>
       <div class="well" id="E2"> <p>$200</p>    </div> 
    </div>

    <div class="row">
       <div class="well" id="A3">  <p>$300</p>   </div>
       <div class="well" id="B3">  <p>$300</p>   </div>
       <div class="well" id="C3">  <p>$300</p>   </div>
       <div class="well" id="D3">  <p>$300</p>   </div>
       <div class="well" id="E3">  <p>$300</p>   </div> 
    </div>
    <div class="row">
       <div class="well" id="A4"> <p>$400</p>     </div>
       <div class="well" id="B4"> <p>$400</p>    </div>
       <div class="well" id="C4"> <p>$400</p>    </div>
       <div class="well" id="D4"> <p>$400</p>    </div>
       <div class="well" id="E4"> <p>$400</p>    </div>   
    </div>
    <div class="row">
       <div class="well" id="A5"><p>$500</p>     </div>
       <div class="well" id="B5"><p>$500</p>      </div>
       <div class="well" id="C5"><p>$500</p>      </div>
       <div class="well" id="D5"><p>$500</p>      </div>
       <div class="well" id="E5"><p>$500</p>      </div> 
    </div>
    <h3> score = </h3>
    <div class = "score">0</div>

</div>

And this is how I'm doing each question.

$("#A1").click(function() {

             $(".question").show();
             $('<h4>The \"MVP\" quarterback whose team is 14-6 when he doesn’t play.</h4>').appendTo('.question');
             $('#submit').click(function() {
                 $("#answer").on('input')
                    var answer = $('#answer').val(); //what does this mean in words? 


                    if 
                        (answer == "Tom Brady" || answer == "Brady" || answer == "brady" || answer == "tom brady") {
                        $('.question').replaceWith('<h3>omg you\'re so smart</h3>')      //using h3 because it'll be unique, but there must be a better way
                        score += questionValue;
                        $(".score").text("$" + score);
                    }
                    else 
                        {
                        $('.question').replaceWith('<h3>could NOT have been more wrong</h3>');
                        score -= questionValue;
                        $(".score").text("$" + score);
         }

}); });

like image 671
Nick Avatar asked Oct 17 '22 21:10

Nick


1 Answers

Whatever you have implemented so far may be correct but I guess its very difficult to maintain that code. Also for any new person t will be quite uneasy to understand that logic. Please have a look at below approach. You may refer it and take an idea out of it:

$(document).ready(function() {
  $(".well").click(function() {
    var quesVal = $(this).data("questionvalue"); //reading the questionvalue associated with each question
    alert(quesVal);
  });
});
.questionContainer {
  width: 250px;
}
.questionContainer .well {
  height: 40px;
  width: 40px;
  border: 1px solid red;
  margin-left: 5px;
  margin-top: 5px;
  float: left;
  /*Here you can also use display: inline-block instead of float:left*/
  background: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="questionContainer">
  <!--Putting question value in each and every box along with id and class as a data- attribute -->
  <div class="well" id="A1" data-questionvalue="100">A1</div>
  <div class="well" id="B1" data-questionvalue="100">B1</div>
  <div class="well" id="C1" data-questionvalue="100">C1</div>
  <div class="well" id="D1" data-questionvalue="100">D1</div>
  <div class="well" id="E1" data-questionvalue="100">E1</div>
  <div class="well" id="A2" data-questionvalue="200">A2</div>
  <div class="well" id="B2" data-questionvalue="200">B2</div>
  <div class="well" id="C2" data-questionvalue="200">C2</div>
  <div class="well" id="D2" data-questionvalue="200">D2</div>
  <div class="well" id="E2" data-questionvalue="200">E2</div>
</div>
like image 87
vijayP Avatar answered Oct 21 '22 03:10

vijayP