www.webdeveloper.com
Results 1 to 10 of 10

Thread: Image onClick

  1. #1
    Join Date
    Apr 2008
    Posts
    64

    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.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Question

    Quote Originally Posted by baldwingrand View Post
    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?

  3. #3
    Join Date
    Apr 2008
    Posts
    64
    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>&nbsp;&nbsp;contains &nbsp;&nbsp;
    			
    			<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>&nbsp;&nbsp;contains &nbsp;&nbsp;
    			
    			<input name="textfield" type="text" id="textfield" size="30" maxlength="30">
    			</p>
    		</div>
    		
    		<div id="divOpenDate" style="padding:3 pt;">
    			<h4>&nbsp;</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>&nbsp;</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>&nbsp;</p>
    </form>
    
    </body>
    </html>
    			

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Lightbulb 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>

  5. #5
    Join Date
    Apr 2008
    Posts
    64
    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?

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255
    Sure.

    What do your script tag IDs look like?
    How many IDs at a time?

  7. #7
    Join Date
    Apr 2008
    Posts
    64
    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'

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255

    Wink 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.

  9. #9
    Join Date
    Apr 2008
    Posts
    64

    resolved

    This works beautifully. Thank you so much for all your help!

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,255
    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
  •  
HTML5 Development Center



Recent Articles