son.of.the.morning Posted December 5, 2011 Share Posted December 5, 2011 I have a while loop fucntion displaying a list of records, each while records contains a hidden div with additional infomation and a button that will call JQuery slideToggle() function. each button on each record toggles alll the hidden divs. Does anyone have any ideas on how to sort this issue? Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/ Share on other sites More sharing options...
gristoi Posted December 5, 2011 Share Posted December 5, 2011 without seeing any code it is going to be extremely hard to tell you what the problem is. but the fact that you are generating this via a loop i would say that you are assigning the click event to the same id every time. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294539 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 Yea thats it exacly, do you know a way around this? Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294541 Share on other sites More sharing options...
gristoi Posted December 5, 2011 Share Posted December 5, 2011 have a look at jquery's .each() function. this will do what you need Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294543 Share on other sites More sharing options...
imperium2335 Posted December 5, 2011 Share Posted December 5, 2011 You could use a counter in your script to add 1 to each id, e.g. myItem0, myItem1 etc, and have your jquery do the same for the reference like $('#myItem'+counter) Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294545 Share on other sites More sharing options...
AyKay47 Posted December 5, 2011 Share Posted December 5, 2011 are you using a singular class for your hidden div? you will want to use unique id's for this and associate each button id with the correct div id. and I recommend using .each as well. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294549 Share on other sites More sharing options...
gristoi Posted December 5, 2011 Share Posted December 5, 2011 example: Â <div class="wibble"></div> <div class="wibble"></div> <div class="wibble"></div> <div class="wibble"></div> $(".wibble").each(function(){ Â $(this).toggle(); }); Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294552 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 I am actauly mind blank on this one... Â PHP & HTML <?php $rowNum = "0"; while($record_rows = mysql_fetch_array($records_returned)){ if(++$rowNum % 2 == 1 ) { ?> Â Â Â Â Â Â Â Â <div class="Record"> Â Â Â Â Â Â Â Â Â Â <div id="VeiwRecord">More Details</div> Â Â Â Â Â Â Â </div> Â Â Â Â Â Â Â Â </div> Â Â Â Â Â Â Â Â //THE HIDDEN DIV Â Â Â Â Â Â Â Â <div id="ViewMoreHidden"><?php echo substr($record_rows['post'], 0,500)."..."; ?></div> Â Â Â Â Â Â Â Â <?php } else {?> Â Â Â Â Â Â Â Â <div class="Record_i"> Â Â Â Â Â Â Â Â Â Â <div id="VeiwRecord">More Details</div> Â Â Â Â Â Â Â </div> Â Â Â Â Â Â Â Â //THE HIDDEN DIV Â Â Â Â Â Â Â Â <div id="ViewMoreHidden"><?php echo substr($record_rows['post'], 0,500)."..."; ?></div> Â Â Â Â Â Â Â Â <?php }} mysql_close();?> Â JQUERY $('document').ready(function() { $('#VeiwRecord').click(function() { $('#ViewMoreHidden').slideToggle(2000); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294553 Share on other sites More sharing options...
gristoi Posted December 5, 2011 Share Posted December 5, 2011 $('document').ready(function() { $('#VeiwRecord').each(function() { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $(this).click(function(){ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $('#ViewMoreHidden', this).slideToggle(2000); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }): }); }); Â havent tested it, but should point you in the right direction Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294556 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 would i have to change any of the php/html? Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294557 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 Just tryied that but it didnt work. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294559 Share on other sites More sharing options...
gristoi Posted December 5, 2011 Share Posted December 5, 2011 sorry, didnt look at your code properly. iterating over the worng thing: Â $('document').ready(function() { $('.Record').each(function() { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $('#VeiwRecord',this).click(function(){ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $('#ViewMoreHidden', this).slideToggle(2000); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }): }); }); Â have a play round with that Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294562 Share on other sites More sharing options...
KevinM1 Posted December 5, 2011 Share Posted December 5, 2011 Remember that ids are supposed to be unique. If you have multiple ViewRecords or ViewMoreHiddens, JavaScript will only 'see' the first one. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294564 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 Still nothing at all :S Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294566 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 Here is the full php/html code  <?php $rowNum = "0"; while($record_rows = mysql_fetch_array($records_returned)){ if(++$rowNum % 2 == 1 ) { ?>         <div class="Record">         <div class="Thumbnail"><img src="../img/articles/thums/<?php echo $record_rows['img_url']; ?>" class="RecordThumbnail"/></div>           <div class="RecordContent">           <div class="PostTitle"><?php echo $record_rows['title']; ?></div>             <div class="PostSnipit"><?php echo substr($record_rows['post'], 0,50)."..."; ?></div>           </div>           <div class="SelectOptions">             <div class="DeleteRecord">Delete Article</div>             <div id="VeiwRecord">More Details</div>           </div>         </div>         <div id="ViewMoreHidden"><?php echo substr($record_rows['post'], 0,500)."..."; ?></div>         <?php } else {?>         <div class="Record_i">         <div class="Thumbnail"><img src="../img/articles/thums/<?php echo $record_rows['img_url']; ?>" class="RecordThumbnail"/></div>           <div class="RecordContent">           <div class="PostTitle"><?php echo $record_rows['title']; ?></div>             <div class="PostSnipit"><?php echo substr($record_rows['post'], 0,50)."..."; ?></div>           </div>           <div class="SelectOptions">             <div class="DeleteRecord">Delete Article</div>             <div id="VeiwRecord">More Details</div>           </div>         </div>         <div id="ViewMoreHidden"><?php echo substr($record_rows['post'], 0,500)."..."; ?></div>         <?php }} mysql_close();?>       </div> Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294568 Share on other sites More sharing options...
gristoi Posted December 5, 2011 Share Posted December 5, 2011 he is correct, id's are meant to be unique, am javascript will only execute the first instance it finds, but using the each function correctly in jquery means you can refer to the Record divs child elements within a local scope. i have tried this locally and it works fine. treating each one individually: <div class="Record"> <div id="VeiwRecord">click me</div> <div id="ViewMoreHidden">Im hidden</div> </div> <div class="Record"> <div id="VeiwRecord">click me</div> <div id="ViewMoreHidden">Im hidden</div> </div> <div class="Record"> <div id="VeiwRecord">click me</div> <div id="ViewMoreHidden">Im hidden</div> </div> <div class="Record"> <div id="VeiwRecord">click me</div> <div id="ViewMoreHidden">Im hidden</div> </div> $('document').ready(function() { $('.Record').each(function() { var obj = $(this); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $('#VeiwRecord',obj).click(function(){ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $('#ViewMoreHidden',obj).slideToggle(2000); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294575 Share on other sites More sharing options...
AyKay47 Posted December 5, 2011 Share Posted December 5, 2011 yes in order for this to work correctly and to avoid some ugly coding, both your button and your hidden div should be included in the same parent div. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294578 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 Got it sorted, thanks everyone for your help. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294584 Share on other sites More sharing options...
KevinM1 Posted December 5, 2011 Share Posted December 5, 2011 gristoi, it may work, but it's semantically wrong. IDs are supposed to be unique for the entire HTML document. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294589 Share on other sites More sharing options...
son.of.the.morning Posted December 5, 2011 Author Share Posted December 5, 2011 Yes your correct, however the same method works fine with class's. Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294595 Share on other sites More sharing options...
AyKay47 Posted December 5, 2011 Share Posted December 5, 2011 Yes your correct, however the same method works fine with class's. as it is meant for classes Quote Link to comment https://forums.phpfreaks.com/topic/252499-jquery-php-problem/#findComment-1294597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.