Image onClick
I would like to use an onClick on a couple of <img> tags to show/hide additional fields. For example- I have images for "+" and "-" signs. When I click on the "+" image, I want additional fields to display, and when I click the "-" image, I want fields to disappear. If this is possible, what would the code look like? Thanks.
Originally Posted by
baldwingrand
I would like to use an onClick on a couple of <img> tags to show/hide additional fields. For example- I have images for "+" and "-" signs. When I click on the "+" image, I want additional fields to display, and when I click the "-" image, I want fields to disappear. If this is possible, what would the code look like? Thanks.
Yes, it's possible.
What code have you attempted so far?
Here's what I have so far... I initially used radio buttons, so I've left them in for the example. I would probably also need to be able to disable the "-" image when only one keyword search box is available. Thanks!
HTML Code:
<html>
<head>
<script type="text/javascript" >
function toggleSearch()
{
toggleDisabled(document.getElementById("divSelect"));
toggleDisabled(document.getElementById("divSelect2"));
toggleDisabled(document.getElementById("cboSelect3"));
}
function displaySelect()
{
divSelect2.style.display = "none";
divMatch2.style.display = "none";
divMatch1.style.display = "";
valid = false;
if(document.frmIssueAdvancedSearch.rbPlusMinus[0].checked)
{
valid = true;
divSelect2.style.display = "";
divMatch2.style.display = "";
divMatch1.style.display = "none";
}
if(document.frmIssueAdvancedSearch.rbPlusMinus[1].checked)
{
valid = true;
divSelect2.style.display = "";
divMatch2.style.display = "";
divMatch1.style.display = "none";
}
}
function toggleDisabled(el)
{
try
{
el.disabled = !el.disabled;
}
catch(E){}
if (el.childNodes && el.childNodes.length > 0)
{
for (var i=0; i<el.childNodes.length; i++)
{
toggleDisabled(el.childNodes[i]);
}
}
}
</script>
</head>
<body>
<form name="frmIssueAdvancedSearch" id="frmIssueAdvancedSearch" method="post" >
<table width="99%" border="1" class="WebApps" >
<tr> <td colspan="2" > <h3> Keyword Search</h3> </td> </tr>
<tr>
<td colspan="2" >
<div id="divMatch1" >
<p> <input name="checkbox" type="checkbox" id="checkbox" value="toggleSearch()" onClick="toggleSearch()" checked> Match the following criteria:</p>
</div>
<div id="divMatch2" style="display:none;" >
<p>
<input name="checkbox2" type="checkbox" id="checkbox2" value="toggleSearch()" onClick="toggleSearch()" checked> Match
<select name="cboCriteria" id="cboCriteria" >
<option selected> all</option>
<option> any</option>
</select> of the following criteria:
</p>
</div>
<div id="divSelect" style="padding:3 pt;" >
<p>
<select name="cboSelect" id="cboSelect" >
<option> Title</option>
<option> Description</option>
<option> Issue Type</option>
<option> Function</option>
</select> contains
<input name="txtKeyword" type="text" id="txtKeyword" size="30" maxlength="30" >
<img src="../images/plus.gif" width="15" height="15" alt="" >
<img src="../images/minus.gif" width="15" height="15" alt="" >
<input type="radio" name="rbPlusMinus" id="rbPlusMinus" value="0" onClick="displaySelect()" > +
<input type="radio" name="rbPlusMinus" id="rbPlusMinus" value="1" onClick="displaySelect()" > -
</p>
</div>
<div id="divSelect2" style="padding:3 pt; display:none;" >
<p>
<select name="cboSelect2" id="cboSelect2" >
<option> Title</option>
<option> Description</option>
<option> Issue Type</option>
<option> Function</option>
</select> contains
<input name="textfield" type="text" id="textfield" size="30" maxlength="30" >
</p>
</div>
<div id="divOpenDate" style="padding:3 pt;" >
<h4> </h4>
<h4> When opened:</h4>
<p>
<select name="cboOpenDate" id="cboOpenDate" >
<option selected> 15 days ago</option>
<option> one month ago</option>
<option> three months ago</option>
</select>
</p>
</div>
<p> </p>
<p>
<input name="button" type="submit" class="button" id="button2" value="Search" >
<input name="Reset" type="reset" class="button" id="button" onClick="parent.location='ip_issues_search.asp'" value="Reset Search" >
</p>
</td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
Try this ...
Substitute your images for the <button> tags
and place your code between the <div> tags that will do what you want when the "+" is pressed.
Code:
<html>
<head>
<title>Show Hide DIV</title>
<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?p=993689#post993689
function Show(IDS,flag) {
document.getElementById(IDS).style.display = flag;
}
</script>
</head>
<body>
<button onclick="Show('divTag','block')">+</button>
<button onclick="Show('divTag','none')">-</button>
<div id="divTag" style="display:none">
Information to display with plus sign pressed
</div>
</body>
</html>
This works very well- but, I have more than one <div> tag I need to show/hide- the "divMatch1" and "divMatch2" - is there a function that will allow for passing more than one div in at a time?
Sure.
What do your script tag IDs look like?
How many IDs at a time?
Well, there are 4, but not sure if I need to reference them all. The general idea is:
if you select "+":
divSelect1 - 'block'
divSelect2 - 'block'
divMatch2 - 'block'
divMatch1 - 'none'
if you select "-":
divMatch1 - 'block'
divSelect1 - 'block'
divSelect2 - 'none'
divMatch2 - 'none'
Try this ...
Will this work for you?
Code:
<html>
<head>
<title>Show Hide DIV</title>
<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?p=993689#post993689
var divOrder = ['divSelect1','divSelect2','divMatch1','divMatch2'];
function Show(info) {
var temp = info.split(',');
for (var i=0; i<temp.length; i++) {
IDS = divOrder[i]; // alert(i+' : '+IDS);
if (temp[i] == 0) {
document.getElementById(IDS).style.display = 'none';
} else {
document.getElementById(IDS).style.display = 'block';
}
}
}
</script>
</head>
<body onLoad="Show('0,0,0,0')">
<button onclick="Show('1,1,0,1')">+</button>
<button onclick="Show('1,0,1,0')">-</button>
<div id="divSelect1" style="display:none">divSelect1</div>
<div id="divSelect2" style="display:none">divSelect2</div>
<div id="divMatch1" style="display:none">divMatch1</div>
<div id="divMatch2" style="display:none">divMatch2</div>
</body>
</html>
Could add a check to assure length of 'info' passed to "Show(info)" function was the same length as the divOrder array. I'll leave that as an exercise for the student.
This works beautifully. Thank you so much for all your help!
You're most welcome.
Glad I was able to help.
Good Luck!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks