Solved

Body OnLoad Value for Jquery show/hide

Posted on 2010-09-02
12
703 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now