Solved

Body OnLoad Value for Jquery show/hide

Posted on 2010-09-02
12
709 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 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