If...Else syntax might be wrong?
- mindfullsilence
- Professor


- Joined: Aug 04, 2008
- Posts: 846
- Status: Offline
I'm using the jQuery "blind" function to expand and hide a div depending on whether or not the checkbox is checked. My if else statement isn't working though.
my html looks something like this:
Anyone know why?
Code: [ Select ]
var y = document.getElementById("advanced").checked;
if (y == true){
$(function(){
$("#advanced").click(function () {
$("#searchTools").show("blind", { direction: "vertical" }, 700);
});
});
}
else {
$(function(){
$("#advanced").click(function () {
$("#searchTools").hide("blind", { direction: "vertical" }, 700);
});
});
}
if (y == true){
$(function(){
$("#advanced").click(function () {
$("#searchTools").show("blind", { direction: "vertical" }, 700);
});
});
}
else {
$(function(){
$("#advanced").click(function () {
$("#searchTools").hide("blind", { direction: "vertical" }, 700);
});
});
}
- var y = document.getElementById("advanced").checked;
- if (y == true){
- $(function(){
- $("#advanced").click(function () {
- $("#searchTools").show("blind", { direction: "vertical" }, 700);
- });
- });
- }
- else {
- $(function(){
- $("#advanced").click(function () {
- $("#searchTools").hide("blind", { direction: "vertical" }, 700);
- });
- });
- }
my html looks something like this:
Code: [ Select ]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MusicBrainz!</title>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.blind.js"></script>
<script type="text/javascript" src="js/divgrow.js"></script>
<style type="css/text">
#seachTools {
display: none;
height: 220px;
width: 450px;
overflow: hidden;
display: none;
}
</head>
<body>
<div id="searchTools">
<label title="Search For">Search For: </label>
<input name="search2" type="text" size="50" maxlength="256" border="0" class="searchInput" />
</div>
<div id="inputWrap">
<label>
<input type="checkbox" id="advanced" />
</label>
<label>Advanced Search Options</label>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MusicBrainz!</title>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.blind.js"></script>
<script type="text/javascript" src="js/divgrow.js"></script>
<style type="css/text">
#seachTools {
display: none;
height: 220px;
width: 450px;
overflow: hidden;
display: none;
}
</head>
<body>
<div id="searchTools">
<label title="Search For">Search For: </label>
<input name="search2" type="text" size="50" maxlength="256" border="0" class="searchInput" />
</div>
<div id="inputWrap">
<label>
<input type="checkbox" id="advanced" />
</label>
<label>Advanced Search Options</label>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>MusicBrainz!</title>
- <link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
- <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
- <script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js"></script>
- <script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.blind.js"></script>
- <script type="text/javascript" src="js/divgrow.js"></script>
- <style type="css/text">
- #seachTools {
- display: none;
- height: 220px;
- width: 450px;
- overflow: hidden;
- display: none;
- }
- </head>
- <body>
- <div id="searchTools">
- <label title="Search For">Search For: </label>
- <input name="search2" type="text" size="50" maxlength="256" border="0" class="searchInput" />
- </div>
- <div id="inputWrap">
- <label>
- <input type="checkbox" id="advanced" />
- </label>
- <label>Advanced Search Options</label>
Anyone know why?
Use your words like arrows to shoot toward your goal.
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
May 13th, 2009, 8:57 pm
- Nightslyr
- Proficient


- Joined: Sep 21, 2005
- Posts: 274
- Status: Offline
Well, first, your code doesn't make much sense from a logical standpoint. You essentially have:
If 'advanced' has been checked:
when 'advanced' is clicked, show an element.
Else:
when 'advanced' is clicked, hide an element.
It's extraneous as a click on 'advanced' is the same as whether or not it's been checked.
Instead, why not tie everything to a toggle?
I've never used the UI component of jQuery, but I don't see why a simple toggle wouldn't suffice.
If 'advanced' has been checked:
when 'advanced' is clicked, show an element.
Else:
when 'advanced' is clicked, hide an element.
It's extraneous as a click on 'advanced' is the same as whether or not it's been checked.
Instead, why not tie everything to a toggle?
Code: [ Select ]
$("#advanced").toggle(
function()
{
$("#searchtools").show("blind", { direction: "vertical" }, 700);
},
function()
{
$("#searchtools").hide("blind", { direction: "vertical" }, 700);
});
function()
{
$("#searchtools").show("blind", { direction: "vertical" }, 700);
},
function()
{
$("#searchtools").hide("blind", { direction: "vertical" }, 700);
});
- $("#advanced").toggle(
- function()
- {
- $("#searchtools").show("blind", { direction: "vertical" }, 700);
- },
- function()
- {
- $("#searchtools").hide("blind", { direction: "vertical" }, 700);
- });
I've never used the UI component of jQuery, but I don't see why a simple toggle wouldn't suffice.
- mindfullsilence
- Professor


- Joined: Aug 04, 2008
- Posts: 846
- Status: Offline
- mindfullsilence
- Professor


- Joined: Aug 04, 2008
- Posts: 846
- Status: Offline
check it out guys, finally got it working. I found a jQuery effect called slideToggle. I'll go ahead and explain what's going on for anyone who needs it.
The code:
The first part of the javascript tells the document to hide the div that has the id "searchTools" before the page loads:
// The first line asks the document if it's ready to load:
$(document).ready(function() {
//When the document is ready to load, but before it actually does,
//this line tell the document to hide the div "searchTools."
$('#searchtools').hide();
This second part initiates when the user has triggered an event:
//This line initiates the next line whenever the user clicks on the checkbox with the id "advanced"
$('#advanced').click(function() {
//This line tells the div with the id "searchTools"
//to unhide with a verticle sliding motion (slideToggle)
//and to take 1000 milliseconds to complete.
$('#searchTools').slideToggle(1000);
Of course this is to my best understanding of a language (javascript) that I've just began to dip into only 2 days ago. So anyone with the knowhow who thinks I've explained this wrong, please feel free to correct me.
The code:
Code: [ Select ]
Javascript Code
$(document).ready(function() {
$('#searchtools').hide();
$('#advanced').click(function() {
$('#searchTools').slideToggle(1000);
return false;
});
});
$(document).ready(function() {
$('#searchtools').hide();
$('#advanced').click(function() {
$('#searchTools').slideToggle(1000);
return false;
});
});
- Javascript Code
- $(document).ready(function() {
- $('#searchtools').hide();
- $('#advanced').click(function() {
- $('#searchTools').slideToggle(1000);
- return false;
- });
- });
Code: [ Select ]
HTML Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MusicBrainz!</title>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.blind.js"></script>
<script type="text/javascript" src="js/divgrow.js"></script>
<style type="css/text">
#seachTools {
display: none;
height: 220px;
width: 450px;
overflow: hidden;
display: none;
}
</head>
<body>
<div id="searchTools">
<label title="Search For">Search For: </label>
<input name="search2" type="text" size="50" maxlength="256" border="0" class="searchInput" />
</div>
<div id="inputWrap">
<label>
<input type="checkbox" id="advanced" />
</label>
<label>Advanced Search Options</label>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MusicBrainz!</title>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js"></script>
<script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.blind.js"></script>
<script type="text/javascript" src="js/divgrow.js"></script>
<style type="css/text">
#seachTools {
display: none;
height: 220px;
width: 450px;
overflow: hidden;
display: none;
}
</head>
<body>
<div id="searchTools">
<label title="Search For">Search For: </label>
<input name="search2" type="text" size="50" maxlength="256" border="0" class="searchInput" />
</div>
<div id="inputWrap">
<label>
<input type="checkbox" id="advanced" />
</label>
<label>Advanced Search Options</label>
- HTML Code:
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>MusicBrainz!</title>
- <link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
- <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
- <script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js"></script>
- <script src="http://dev.jquery.com/view/tags/ui/latest/ui/effects.blind.js"></script>
- <script type="text/javascript" src="js/divgrow.js"></script>
- <style type="css/text">
- #seachTools {
- display: none;
- height: 220px;
- width: 450px;
- overflow: hidden;
- display: none;
- }
- </head>
- <body>
- <div id="searchTools">
- <label title="Search For">Search For: </label>
- <input name="search2" type="text" size="50" maxlength="256" border="0" class="searchInput" />
- </div>
- <div id="inputWrap">
- <label>
- <input type="checkbox" id="advanced" />
- </label>
- <label>Advanced Search Options</label>
The first part of the javascript tells the document to hide the div that has the id "searchTools" before the page loads:
Code: [ Select ]
// The first line asks the document if it's ready to load:
$(document).ready(function() {
//When the document is ready to load, but before it actually does,
//this line tell the document to hide the div "searchTools."
$('#searchtools').hide();
- // The first line asks the document if it's ready to load:
- $(document).ready(function() {
- //When the document is ready to load, but before it actually does,
- //this line tell the document to hide the div "searchTools."
- $('#searchtools').hide();
This second part initiates when the user has triggered an event:
Code: [ Select ]
//This line initiates the next line whenever the user clicks on the checkbox with the id "advanced"
$('#advanced').click(function() {
//This line tells the div with the id "searchTools"
//to unhide with a verticle sliding motion (slideToggle)
//and to take 1000 milliseconds to complete.
$('#searchTools').slideToggle(1000);
- //This line initiates the next line whenever the user clicks on the checkbox with the id "advanced"
- $('#advanced').click(function() {
- //This line tells the div with the id "searchTools"
- //to unhide with a verticle sliding motion (slideToggle)
- //and to take 1000 milliseconds to complete.
- $('#searchTools').slideToggle(1000);
Of course this is to my best understanding of a language (javascript) that I've just began to dip into only 2 days ago. So anyone with the knowhow who thinks I've explained this wrong, please feel free to correct me.
Use your words like arrows to shoot toward your goal.
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 4 posts
- Users browsing this forum: No registered users and 119 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
