473,387 Members | 1,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Accordion and ajax

5
Hi,
im using an accordion script but it doesnt work if i use it with ajax.
example:
Expand|Select|Wrap|Line Numbers
  1. <div id="accordion">
  2.      <dl class="accordion" id="slider">
  3.            <dt>click here for the 1st pane</dt>
  4.                  <dd>content of the 1st pane</dd>
  5.            <dt>click here for the 1st pane</dt>
  6.                  <dd>content of the 1st pane</dd>
  7.            <dt>click here for the 1st pane</dt>
  8.                  <dd>content of the 1st pane</dd>
  9.      </dl>
  10. </div>
works ok, but if i do:
Expand|Select|Wrap|Line Numbers
  1. <div id="accordion">
  2.      <dl class="accordion" id="slider">
  3. <?
  4. foreach ($array as $n)
  5.           {
  6.           <dt>click here for the $n pane</dt>
  7.                  <dd>content of $n </dd>
  8.            }
  9. ?>
  10.      </dl>
  11. </div>
it works only for the first page, but when i use the ajax to reload the foreach statement, when i click the script it does nothing, not even an error and the panes are all collapsed.
it seems that the script lack of an onclick function in the <dt> pane, instead the onclick function that's triggered from within the script.


accordion.js
Expand|Select|Wrap|Line Numbers
  1. var accordion=function(){
  2.     var tm=sp=10;
  3.     function slider(n){this.nm=n; this.arr=[]}
  4.     slider.prototype.init=function(t,c,k){
  5.         var a,h,s,l,i; a=document.getElementById(t); this.sl=k?k:'';
  6.         h=a.getElementsByTagName('dt'); s=a.getElementsByTagName('dd'); this.l=h.length;
  7.         for(i=0;i<this.l;i++){var d=h[i]; this.arr[i]=d; d.onclick=new Function(this.nm+'.pro(this)'); if(c==i){d.className=this.sl}}
  8.         l=s.length;
  9.         for(i=0;i<l;i++){var d=s[i]; d.mh=d.offsetHeight; if(c!=i){d.style.height=0; d.style.display='none'}}
  10.     }
  11.     slider.prototype.pro=function(d){
  12.         for(var i=0;i<this.l;i++){
  13.             var h=this.arr[i], s=h.nextSibling; s=s.nodeType!=1?s.nextSibling:s; clearInterval(s.tm);
  14.             if(h==d&&s.style.display=='none'){s.style.display=''; su(s,1); h.className=this.sl}
  15.             else if(s.style.display==''){su(s,-1); h.className=''}
  16.         }
  17.     }
  18.     function su(c,f){c.tm=setInterval(function(){sl(c,f)},tm)}
  19.     function sl(c,f){
  20.         var h=c.offsetHeight, m=c.mh, d=f==1?m-h:h; c.style.height=h+(Math.ceil(d/sp)*f)+'px';
  21.         c.style.opacity=h/m; c.style.filter='alpha(opacity='+h*100/m+')';
  22.         if(f==1&&h>=m){clearInterval(c.tm)}else if(f!=1&&h==1){c.style.display='none'; clearInterval(c.tm)}
  23.     }
  24.     return{slider:slider}
  25. }();
HTML
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>JavaScript Accordion</title>
  6. <link rel="stylesheet" href="style.css" type="text/css" />
  7. <script type="text/javascript" src="accordion.js"></script>
  8. </head>
  9. <body>
  10. <div id="accordion">
  11.     <dl class="accordion" id="slider">
  12.         <dt>CLICK ME</dt>
  13.                <dd>test </dd>
  14.         <dt>CLICK ME</dt>
  15.                 <dd>test 2   </dd>
  16.         <dt>CLICK ME</dt>
  17.                   <dd>Test 3    </dd>
  18.     </dl>
  19. </div>
  20.  
  21. <script type="text/javascript">
  22. var slider1=new accordion.slider("slider1");
  23. slider1.init("slider");
  24. </script>
  25.  
  26. </body>
  27. </html>
style.css
Expand|Select|Wrap|Line Numbers
  1. * {margin:0; padding:0}
  2.  
  3. #accordion {width:459px; margin:50px auto}
  4. .accordion {width:459px; font:12px Verdana,Arial; color:#033}
  5. .accordion dt {width:439px; border:2px solid #9ac1c9; padding:8px; font-weight:bold; margin-top:5px; cursor:pointer; background:url(images/header.gif)}
  6. .accordion dt:hover {background:url(images/header_over.gif)}
  7. .accordion dd {overflow:hidden; background:#fff}
  8. .accordion span {display:block; width:425px; border:2px solid #9ac1c9; border-top:none; padding:15px}
  9.  
  10. #accordion2 {width:259px; margin:50px auto; border:1px solid #333; border-top:none}
  11. .accordion2 {width:259px; font:12px Verdana,Arial; color:#333}
  12. .accordion2 dt {width:247px; padding:4px 6px; font-weight:bold; cursor:pointer; background-color:#666; background-image:url(images/arrow_down.gif); background-position:right center; background-repeat:no-repeat; color:#fff; border-top:1px solid #333}
  13. .accordion2 dt:hover {background-color:#555}
  14. .accordion2 .open {background-color:#444; background-image:url(images/arrow_up.gif)}
  15. .accordion2 dd {overflow:hidden; background:#fff}
  16. .accordion2 span {display:block; width:229px; border-top:none; padding:15px}
So my question is:
would it be possible to create an onClick function, like "onClick=open_pane($pane_id)", instead of how is doing now?

Thanks.
Jan 17 '09 #1
8 3582
acoder
16,027 Expert Mod 8TB
Can you show the Ajax code that rewrites the foreach statement.
Jan 18 '09 #2
quipo
5
sure,
i use the basic (only 1k) 'cos i dont need the fancy stuff of jquery, mootools, etc..., i also tried to wrap the whole accordion.js with a "function abc(){
$#!$@#
}, but no luck.



ajax.js
Expand|Select|Wrap|Line Numbers
  1. function ajaxFunction(_1,_2,pag,res_div,ldr){
  2. //    alert(ldr);
  3. var vi = document.getElementById(ldr);
  4. vi.style.visibility = "visible";
  5. var _3;
  6. try{
  7. _3=new XMLHttpRequest();
  8. }
  9. catch(e){
  10. try{
  11. _3=new ActiveXObject("Msxml2.XMLHTTP");
  12. }
  13. catch(e){
  14. try{
  15. _3=new ActiveXObject("Microsoft.XMLHTTP");
  16. }
  17. catch(e){
  18. alert("Your browser broke!Need faster, safer, better browser? Get Firefox! Free!");
  19. return false;
  20. }}}
  21. _3.onreadystatechange=function(){
  22. var _4=document.getElementById(res_div);
  23. if(_3.readyState==4){
  24. vi.style.visibility = "hidden";
  25.  _4.innerHTML=_3.responseText;
  26. };
  27. var _5="?var1="+_1+"&var2="+_2;
  28. _3.open('GET',pag+_5,true);
  29. _3.send(null);
  30. };
  31.  
Jan 18 '09 #3
quipo
5
forgot the link.
i use : <a href="#" onclick="ajaxFunction('var1','var2','page_to_query .php','result_div','loader_div')">
click for ajax</a>
i like 'cos extremely simple, light, efficient.
Jan 18 '09 #4
acoder
16,027 Expert Mod 8TB
This Ajax code obviously doesn't appear in the HTML code that you posted earlier. Can you post that version of the page? I'd also like to see what page_to_query returns and the div "result_div". A link would probably be better in this case.
Jan 18 '09 #5
quipo
5
hey men,
is all here, test page and files:
JavaScript Accordion
i still think it's a js issue, however thanks for your time, whatever the result.
Jan 18 '09 #6
acoder
16,027 Expert Mod 8TB
In your test.php file, you've got a div with id "accordion" which will cause problems (duplicate IDs).

Also, since you're overwriting content, you will have to set up the accordion again.
Jan 19 '09 #7
quipo
5
this way "sort of" works", just added an (ugly) onclick function, but stays open.
JavaScript Accordion

'ssorait, getting kinda tired of it.......
Jan 19 '09 #8
acoder
16,027 Expert Mod 8TB
You have two possibilities:

1. Add the init statement after the content is replaced in the Ajax function; or
2. Modify the Ajax function to replace each dt/dd element individually.
Jan 19 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: srilakshmim | last post by:
Hello I want to create Accordion (Ajax Control Tool Kit). My Code is as follows protected void Page_Load(object sender, EventArgs e) { try { Accordion...
3
by: srilakshmim | last post by:
Hello I want to create Accordion (Ajax Control Tool Kit)dynamically. My Code is as follows Code: ( cpp ) protected void Page_Load(object sender, EventArgs e) { try ...
1
by: John Graham | last post by:
I have a form where I separate several of the sections with the use of an accordion. Because of the requirements I have, I need the form to be saved every time the user clicks on a different pane...
7
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
We are looking at Ajaxing our existing web application. Question is...Should we Ajax any and everything we can, or not? One example...if a page posts back to itself, is that a good candidate for...
1
by: jrcapp | last post by:
Let's see if I can clearly explain what I am doing with AJAX. On my Web site, I have a TabContainer that has 2 TabPanels. Inside each Panel, I am dynamically (using a SQL query) creating an...
1
by: Amzul | last post by:
hello all i post the same Q in php forum i believe here its more suitable. was wondering if anyone have a snipt of accordion in ajax/php before i have to dig in the .net section does it have...
4
by: ameshkin | last post by:
Hi Everybody, I'm just now learning javascript and I'm using mootools. What I want to do is to dynamically load a php page into accordian panel 2, depending on which radio button is selected in...
8
by: Tomasz J | last post by:
Hello developers, After migrating my web project application (using the old model) to .Net Framework 3.5 and Ajax Control Toolkit release 20820 the Accordion control no longer works correctly....
1
by: E11esar | last post by:
Hi there. I have created an asp.net page with C# and have added two tabs using the respective AJAX control. I am trying to add a table within an AJAX Accordion control in the second tab but I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.