Solved

Body OnLoad Value for Jquery show/hide

Posted on 2010-09-02
12
706 Views
Last Modified: 2012-05-10
This is a 2 part question:

1) I have a show/hide that is based on whether or not a specific selection is made.

<select name="newUserRoleID" onChange="showHide(this.value)">
        <cfloop query="qry_getRoles">
        <option value="#userRoleID#" <cfif userRoleID eq CurrentUserRole>selected</cfif>>#qry_getRoles.userRole#</option>
        </cfloop>
</select>

<!--- if 3 is selected show this div --->
<div id="3" style="display:none"><cfinclude template="dsp_hierarchyTree.cfm"></div>

<SCRIPT LANGUAGE="javascript" src="../js/jquery.js"></script>
<script language="javascript">

      function showHide(obj)
            {
                  var showDiv = "#"+obj;
                  $("div").hide();
                  $(showDiv).show();
            }

</script>

The problem is sometimes the '3' will be already selected (as this is an edit page). How can I show the div when 3 is the value of CurrentUserRole when the page is loaded?

2) I would like to show this div whether it is 3 OR 4 that are selected. Is it possible to do this for more than one value?

Thanks!
0
Comment
Question by:traport
12 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 33591493
0
 

Author Comment

by:traport
ID: 33591535
Thanks sam2912, but I have no idea what I would do w/ this code (in your links) to answer the issues I'm having. Perhaps I'm missing something?
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33591548
Sorry about the previous comment; it was for another question.

Regarding your questions:
1)
$(document).ready(function(){
      showHide($('select[name=newUserRoleID]').val());
});


2)
Do not understand your second part. Do you mean multiple select fields controlling the display of the div, or having different values from the single select field triggering the function?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:traport
ID: 33591588
Whew... I was wondering if I was losing my mind.

2nd part of the question - where I have

<div id="3" style="display:none"><cfinclude template="dsp_hierarchyTree.cfm"></div>

I want it to be div id="3 OR 4"... I know that's not how I would word it, but I'm not sure how to multiple values there (so either 3 or 4 being selected triggering the function).
0
 
LVL 6

Expert Comment

by:baretree
ID: 33591604
I guess the second question is in regards of, you know, his DIV element has an ID = 3 so, when the selected role is 3 then he shows the DIV but he wants to also show that DIV element if the user selects the role 4, so that is not possible if you're using that property and you want to do it without any hard-code or something.
I don't know if you can have that "option" within your query, so that when you are filling the <select> tag, you can as well, indicate that for that <option> you are going to SHOW or HIDE the DIV element.
So, that way you won't depend exclusively on the DIV's ID
0
 
LVL 14

Expert Comment

by:sam2912
ID: 33591661
If you meant if either 3 or 4 was passed into the function, show that particular div:
function showHide(num)  {
   
   if(parseInt(num)==3 || parseInt(num)==4) {
      $("div").hide();
      $("#3").show(); // Your current div id. Change if necessary.
   }
}

Open in new window

0
 

Author Comment

by:traport
ID: 33591899
Sam2912, using this code doesn't work exactly right. It does work for 3 OR 4 now (thanks!) But the show/hide no longer toggles. Sorry to keep bothering you - I really appreciate your help.
<script language="javascript">
$(document).ready(function(){
      showHide($('select[name=newUserRoleID]').val());
});

      function showHide(obj) 
            {
                  var showDiv = "#"+obj;
                  $("div").hide();
                  $(showDiv).show();
            }

function showHide(num)  {
   
   if(parseInt(num)==3 || parseInt(num)==4) {
      $("div").hide();
      $("#3").show(); // Your current div id. Change if necessary.
   }
}

</script>

Open in new window

0
 
LVL 14

Expert Comment

by:sam2912
ID: 33592018
You can't have both functions. Remove the first?
0
 

Author Comment

by:traport
ID: 33592149
Yes. Sorry about that. I had removed the one but then put it back. It doesn't toggle with the 2nd one (with the 1st gone).
0
 
LVL 14

Accepted Solution

by:
sam2912 earned 250 total points
ID: 33592791
I'm not sure how many divs with numbered IDs you have, but you can try this:
function showHide(num)  {
   
   var showDiv = "#"+parseInt(num);
   if(parseInt(num)==3 || parseInt(num)==4) {
      $("div").hide();
      $("#3").show(); // Your current div id. Change if necessary.
   }
   $(showDiv).show();
}

Open in new window

0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 250 total points
ID: 33593630
According to the earlier function you have mentioned
  1. Hide the div irrespective of what is selected with id ="div"
  2. Show the div with the id chosen from the drop down
  3. if selected if is 3 or 4 show div with id= "3
So a slight modification in the last post.

function showHide(num)
{
	var showDiv = "#"+parseInt(num);
	$("div").hide();
	if(parseInt(num)==3 || parseInt(num)==4)
	{
		$("#3").show(); // Your current div id. Change if necessary.
	}
	else
	{
		$(showDiv).show();
	}	
}

Open in new window

0
 

Author Closing Comment

by:traport
ID: 33595163
Had to split the points b/c Sam spent so much time with me. Ultimately kadaba's worked (Sam the last one still didn't toggle). Appreciate both of you helping. Excellent!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question