?
Solved

Body OnLoad Value for Jquery show/hide

Posted on 2010-09-02
12
Medium Priority
?
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

764 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