Problem with jquery logic

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2009
Posts: 5
Reputation: jaykom is an unknown quantity at this point 
Solved Threads: 0
jaykom jaykom is offline Offline
Newbie Poster

Problem with jquery logic

 
0
  #1
Jun 14th, 2009
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. $(document).ready(function() {
  2. var childrenEle;
  3. function toggleReq() {
  4. $('fieldset.fieldsetClass').each(function() {
  5. id = $(this).attr('id');
  6. $("#" + id).children().not("div,legend").each(function () {
  7. var divEle = $("div").attr('id');
  8. if($(this).attr('class') == 'required') {
  9. $(this).css("display","block");
  10. }
  11. else {
  12. $(this).css("display","none");
  13. }
  14. });
  15.  
  16. });
  17. }
  18. function toggleAll() {
  19. $('fieldset.fieldsetClass').each(function() {
  20. id = $(this).attr('id');
  21. $("#" + id).children().each(function () { $(this).css("display","block");
  22. });
  23. });
  24. }
  25.  
  26. $("div").toggle(
  27. function toggle() {
  28. $('fieldset.fieldsetClass').each(function() {
  29. id = $(this).attr('id');
  30. $("#" + id).children().each(function () { $(this).css("display","block");
  31. });
  32. });
  33. },
  34. function() {
  35. $('fieldset.fieldsetClass').each(function() {
  36. id = $(this).attr('id');
  37. $("#" + id).children().not("div,legend").each(function () {
  38.  
  39. if($(this).attr('class') == 'required') {
  40. $(this).css("display","block");
  41. }
  42. else {
  43. $(this).css("display","none");
  44. }
  45. });
  46. });
  47. }
  48. );
  49.  
  50. /*$("div").toggle(function(){
  51. toggleAll()
  52. },function(){
  53. toggleReq()
  54. }); */
  55. $.onload = toggleReq();
  56. });
  57. </script>
  58. </head>
  59. <body>
  60. <form name="formID" id="questions-form" class="formular" method="post" action="">
  61. <fieldset class="fieldsetClass" id="question-0">
  62. <legend>Question 1</legend><div id="toggle1">Click Here</div>
  63. <input class="required" type="radio" name="answer[0]" value="0" id="radio-0-0" /><label class="required" for="radio-0-0">Green</label>
  64. <input class="required" type="radio" name="answer[0]" value="1" id="radio-0-1" /><label class="required" for="radio-0-1">Red</label>
  65. <input type="radio" name="answer[0]" value="2" id="radio-0-2" /><label for="radio-0-2">Orange</label>
  66. </fieldset>
  67. <fieldset class="fieldsetClass" id="question-1"><div id="toggle2">Click Here</div>
  68. <legend>Question 2</legend>
  69. <input type="radio" name="answer[1]" value="0" id="radio-1-0" /><label for="radio-1-0">21</label>
  70. <input class="required" type="radio" name="answer[1]" value="1" id="radio-1-1" /><label class="required" for="radio-1-1">11</label>
  71. <input class="required" type="radio" name="answer[1]" value="2" id="radio-1-2" /><label class="required" for="radio-1-2">23</label>
  72. </fieldset>
  73. </form>
Hi Folks, From the pasted above code I am trying to loop through every fieldset element with jquery each function, and again iam using one more jquery each function for looping through the every child element - this is the common idea for looping both the functions - toggleReq(), toggleAll().

In toggleReq() through looping Iam segregating the child elements which has got CLASS ATTRIBUTE value as "REQUIRED" and displaying them and hiding rest of the child elements.

So during page onload iam displaying only the child elements which have got CLASS ATTRIBUTE value as "REQUIRED" thru jquery onload.

In toggleAll() iam looping through all child elements and displaying all the required and optional child elements even.

I hope now you guys got the explanations for both the functions

But there is one trap here, the thing is like there should be one clickable icon for every fieldset, so when the user clicks the
icon it should display all the child elements and when click again it should display only the REQUIRED fields, basically like toggling and this toggling should happen only for one fieldset element at a time.

In my code this toggling happens simultaneously for all the fieldset elements and this is not the correct one.

Please any one help me out how to make this toggling function should work independent for the fieldset elements.

please help me out with some ideas for this..

Thanks
jaykom
Last edited by Tekmaven; Jun 15th, 2009 at 1:42 am. Reason: correction in the grammer [Mod Edit: Code Tags]
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 149
Reputation: mschroeder is on a distinguished road 
Solved Threads: 25
mschroeder mschroeder is offline Offline
Junior Poster

Re: Problem with jquery logic

 
0
  #2
Jun 17th, 2009
I think I have achieved what you were describing. There is some consolidation that could occur as there is minor repetitive code.

  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=utf-8" />
  5. <title>Untitled Document</title>
  6. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  7. <script type="text/javascript">
  8. $(document).ready(function() {
  9.  
  10. function toggleReq()
  11. {
  12. $(':input.required').each( function() {
  13. $(this).show();
  14. $(this).next('label.required').show();
  15. });
  16. }
  17.  
  18. $('.toggle').toggle(
  19. function() {
  20. var children = $(this).parent('fieldset').children(':input');
  21. $(children).each(function() {
  22. $(this).show();
  23. $(this).next('label').show();
  24. });
  25. },
  26. function () {
  27. var children = $(this).parent('fieldset').children(':input');
  28. $(children).each(function() {
  29. if( !$(this).hasClass('required') )
  30. {
  31. $(this).hide();
  32. $(this).next('label').hide();
  33. }
  34. });
  35. }
  36. );
  37.  
  38.  
  39. $.onload = toggleReq();
  40. });
  41. </script>
  42. <style>
  43. input
  44. {
  45. display: none;
  46. }
  47. label
  48. {
  49. display: none;
  50. }
  51. </style>
  52. </head>
  53.  
  54. <body>
  55. <form name="formID" id="questions-form" class="formular" method="post" action="">
  56.  
  57. <fieldset class="fieldsetClass" id="question-0">
  58. <legend>Question 1</legend>
  59. <div class="toggle">Click Here</div>
  60.  
  61. <input class="required" type="radio" name="answer[0]" value="0" id="radio-0-0" />
  62. <label class="required" for="radio-0-0">Green</label>
  63.  
  64. <input class="required" type="radio" name="answer[0]" value="1" id="radio-0-1" />
  65. <label class="required" for="radio-0-1">Red</label>
  66.  
  67. <input type="radio" name="answer[0]" value="2" id="radio-0-2" />
  68. <label for="radio-0-2">Orange</label>
  69. </fieldset>
  70.  
  71. <fieldset class="fieldsetClass" id="question-1">
  72. <legend>Question 2</legend>
  73. <div class="toggle">Click Here</div>
  74.  
  75. <input type="radio" name="answer[1]" value="0" id="radio-1-0" />
  76. <label for="radio-1-0">21</label>
  77.  
  78. <input class="required" type="radio" name="answer[1]" value="1" id="radio-1-1" />
  79. <label class="required" for="radio-1-1">11</label>
  80.  
  81. <input class="required" type="radio" name="answer[1]" value="2" id="radio-1-2" />
  82. <label class="required" for="radio-1-2">23</label>
  83. </fieldset>
  84.  
  85. </form>
  86. </body>
  87. </html>

I didnt make any changes to your form EXCEPT i changed the toggle divs to have a class of toggle instead of unique id's. This allows us to bind the same toggle event to every toggle div.

I also set the styles for input and label display:none right off the bat and then only turn on the elements with the class "required".

The rest of it is just basic jquery function calls.
I tested it locally and get the functionality i believe you described so have at it and i'll try to answer any questions you might have regarding the code.

I think if i was to streamline this even further, I would switch the order of the toggle functions and then simply trigger the toggle event on page load instead of having two separate methods...but this is just a proof of concept.
If you're question/problem is solved don't forget to mark the thread as Solved!

-- Code I post is usually but not always tested. If it is tested it will be against 5.2.12 or 5.3.1
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 848 | Replies: 1
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC