Body OnLoad Value for Jquery show/hide

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!
traportAsked:
Who is Participating?
 
Samuel LiewConnect With a Mentor Commented:
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
 
traportAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Samuel LiewCommented:
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
 
traportAuthor Commented:
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
 
baretreeCommented:
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
 
Samuel LiewCommented:
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
 
traportAuthor Commented:
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
 
Samuel LiewCommented:
You can't have both functions. Remove the first?
0
 
traportAuthor Commented:
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
 
kadabaConnect With a Mentor Commented:
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
 
traportAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.