Solved

Function toggle on check-uncheck

Posted on 2011-03-20
12
596 Views
Last Modified: 2012-05-11
Hello experts.
I need help to handle a toggle function when i check - uncheck a check box.
Below is my test code:
My checkbox:
<input name="SameAsBillingInfo" type="checkbox" id="SameAsBillingInfo" value="SameAsBillingInfo" >

the div for toggle:
 <div id="shippinginformation">
..........
...........
</div>


Any help?
0
Comment
Question by:Panos
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 300 total points
ID: 35176062
check this :
<script language="javascript">
   function toggle(o, id) {
       document.getElementById(id).style.display = (o.checked)?"none":"block";
   }
</script>

<input name="SameAsBillingInfo" type="checkbox" id="SameAsBillingInfo" value="SameAsBillingInfo" onclick="toggle(this,'SameAsBillingInfo')" >

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 300 total points
ID: 35176069
with jquery :


$("#SameAsBillingInfo").live("click", function() {
   if( $(this).is(":checked") ) {
      $("#shippinginformation").hide();
   }
   else {
      $("#shippinginformation").show();
   }
})

<input name="SameAsBillingInfo" type="checkbox" id="SameAsBillingInfo" value="SameAsBillingInfo" >

Open in new window

0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 35176072
try this:
<input name="SameAsBillingInfo" type="checkbox" id="SameAsBillingInfo" value="SameAsBillingInfo"  onClick="ToggleCheckhandler(this)">

...
function togglecheckhandler(obj)
{
if (obj.checked)
....
else
....
}

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 2

Author Comment

by:Panos
ID: 35176163
Hi leakim971.
Your codes are working (i have to change only the this element in onclick function).


Maverick cool can you write the full code.


Please add a fade in- out function too.
0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 35176168
k, you want the fade in and out for div rite, give me couple of min i will post it
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
ID: 35176170
replace .hide() by fadeOut("slow");
and .show() by fadeIn("slow");

you may replace "slow" by 1000 or "fast" or 100 or 2000 and so on
0
 
LVL 7

Assisted Solution

by:Maverick_Cool
Maverick_Cool earned 200 total points
ID: 35176191
     // Will fadein if already fadeout
      // will fadeout if aready fadein
var TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}



function togglecheckhandler()
{
	// Will fadein if already fadeout
	// will fadeout if aready fadein
	fade("shippinginformation")
}

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 35176208
Thank you both for your codes!!
0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 35176215
you can also use jquery to make code simpler.
function togglecheckhandler(obj)
{
	if(obj.checked)
	{	$("#shippinginformation").fadeIn("slow");}
	else
	{	$("#shippinginformation").fadeOut("slow");}
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:Panos
ID: 35176220
Thank you again.
regards
panos
0
 
LVL 2

Author Comment

by:Panos
ID: 35176225
Maverick_Cool
I missed your last comment.
Sorry
0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 35176244
no problem.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

627 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