0

I'm trying to create a code that makes one of two panels slide down when a button is pressed. Here's my code:

http://jsfiddle.net/06szhymc/82/

   $(document).ready(function(){
    $("#imageslider").click(function(){
  rand = Math.floor(Math.random() * 2) + 1;
  then if(rand==1){
$("#panel").slideToggle("slow");
        }else{
$("#panel2").slideToggle("slow");
    });
});
       
<button id="imageslider">Press<button>
    <div id="panel"><div id="panel"><div class="paneltest" style="background-color:black; color:white; padding:20px;">

<h2>Paris</h2>

<p>Paris is in France</p>
<div id="div1" style="width:80px;height:80px;background-color:green;"></div>
<p>Standing on the Seine Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>

</div> </div></div>
    
    <div id="panel2"><div id="panel2"><div id="paneltest" style="background-color:black; color:white; padding:20px;">

<h2>London</h2>

<p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
<div id="div1" style="width:80px;height:80px;background-color:red;"></div>
<p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>

</div> </div></div>

I would also like to have the panels be invisible (display:none) before the button is pressed, so that one or the other appears.

I can't see what's wrong with my code. Any help would deb much appreciated.

J N
  • 3
  • 1

1 Answers1

0

You have mixed up many tags.

First your javascript closing of functions are not taken care properly. Try and use an IDE if you are a beginner to understand if you have any missing braces.

Second with your html, you have to understand how to enclose the elements in one another because, what you have there is something with two same id's and a button is not closed properly.

$(document).ready(function() {
  $("#imageslider").click(function() {
    rand = Math.floor(Math.random() * 2) + 1;
    if (rand == 1) {
      $("#panel").slideToggle("slow");
    } else {
      $("#panel2").slideToggle("slow");
    };
  })
});
<button id="imageslider">Press</button>
<div id="panel1">
  <div class="paneltest" style="background-color:black; color:white; padding:20px;">
    <h2>Paris</h2>
    <p>Paris is in France</p>
    <div id="div1" style="width:80px;height:80px;background-color:green;"></div>
    <p>Standing on the Seine Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
  </div>
</div>
<div id="panel2">
  <div id="paneltest" style="background-color:black; color:white; padding:20px;">
    <h2>London</h2>
    <p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
    <div id="div1" style="width:80px;height:80px;background-color:red;"></div>
    <p>Standing on the River Thames, London has been a major settlement for two millennia, its history going back to its founding by the Romans, who named it Londinium.</p>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

For ensuring the div's are invisible at first, you can add

$(document).ready(function () {
    $("#panel1").hide();
    $("#panel2").hide();
 ..
 ..

Update Here is what you can do to get one toggle on another just on visibility

$(document).ready(function () {
    $("#panel1").hide();
    $("#panel2").hide();
    $("#imageslider").click(function () {
        if (!($('#panel1').is(':visible'))) {
            $("#panel1").slideDown("slow");
            $("#panel2").hide();
        } else if (!($('#panel2').is(':visible'))) {
            $("#panel2").slideDown("slow");
            $("#panel1").hide();
        }
    });
});
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78
  • There is nothing wrong, its bascally, you are looping based on `Math.random` and trying your luck to get `1` every time. Which is something not predictable. If you want both of them to toggle, think of condition where your `if` statement can execute. – Nagaraj Tantri May 10 '15 at 02:13
  • I used your guidance to get it close to what I want (http://jsfiddle.net/xq2241j6/11/) but each time the button is pressed, the panels continue to slide down until every panel is displayed. I'm now working on adding "&&" conditions to the if/else statements. I feel as though I need to set the conditions such that they say something along the lines "if (rand == 1 && [#panel2 is hidden]//< – J N May 10 '15 at 02:26
  • Glad to know my answer has helped you. For `[#panel2 is hidden]<< – Nagaraj Tantri May 10 '15 at 02:29
  • I'm so sorry to keep bothering you - last time hopefully. I added the multiple conditions to the if/then statements, and it just totally stopped it from working now. Could you please take a quick glance at what I did wrong? http://jsfiddle.net/xq2241j6/25/ – J N May 10 '15 at 02:41
  • Firstly, no need to be sorry :), secondly, again you have missing braces in javascript and had multiple ready function. Then now see my update. Thirdly, if you have found the answer helpful, do select so. ;) – Nagaraj Tantri May 10 '15 at 03:22
  • Awesome! It works. I tried to up-vote your answer, but it said that I need a "reputation of 15" to do so – J N May 10 '15 at 18:02