Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:44:45+00:00 2026-05-26T18:44:45+00:00

<?php echo(var answersC = answer-table-$qrow->id;); echo(var toggleC = toggle-table-$qrow->id;); ?> jQuery(‘toggleC’).click(function() { jQuery(‘answersC’).slideToggle(‘fast’); });

  • 0
<?php
   echo("var answersC = answer-table-$qrow->id;");
   echo("var toggleC = toggle-table-$qrow->id;");
?>

   jQuery('toggleC').click(function() {
      jQuery('answersC').slideToggle('fast');
});

Instead of having a static css class name, I have a page that creates unique classes in a loop. I’m hoping to be able to toggle these individual areas, but I don’t know how to use the variable string as the name of the css class.

I also tried it like this with no better luck.

<?php
   echo("var answersC = $qrow->id;");
   echo("var toggleC = $qrow->id;");
?>

   jQuery('.answer-table-'+answerC).click(function() {
      jQuery('answersC'+toggleC).slideToggle('fast');
});

Anyone have any ideas? I don’t know enough to get this working. Thanks!

UPDATE: I should add that this is on a PHP page within script tags.

UPDATE 2:

I’ve been trying the various suggestions to the point that my eyeballs feel like they’re holding back a head full of scrambled eggs. I’m pretty good at googling js solutions, but I don’t have a very wide knowledge of it so it’s hard to understand how to expand on them when they don’t work.

Where I am now is this code: (note I switched from classes to ids)

<script type="text/javascript" language="javascript">
    <!--
    <?php
    echo("var answersC = 'answertable'+$qrow->id;");
    echo("var toggleC = 'toggletable'+$qrow->id;");
    ?>
    jQuery('#'+toggleC).click(function() {
    jQuery('#'+answersC).slideToggle('fast');
    });
    console.log('#'+toggleC); //example #toggletable6
    console.log('#'+answersC); //example #answertable6
    // -->
    </script>

I can see in firebug that the variable content matches the ids in the html and I’m getting no errors, but it still does nothing.

Elsewhere, I am using the following functions to display all answers for all questions:

$('.atoggle').click(function() {
$('.answer-table').slideToggle('fast');
});

This one works perfectly, only I really want to only open answers on a per question basis.

Note: The ‘$’ vs. ‘jQuery’ difference is only because one is in a .js file within a wrapper function to allow the ‘$’ within WordPress, the one that’s not working is simply within the page source at this point. Big thanks to everyone for their input!

UPDATE (final):

For anyone who comes across this in a search, I’ll post the final working code. There was an additional problem getting this to work that required wrapping everything in a document.ready function.

The working version:

<script type="text/javascript" language="javascript">
    jQuery(document).ready(function() {
    <!--
        <?php
        echo("var answersC = 'answertable'+$qrow->id;");
        echo("var newanswersC = 'newanswertable'+$qrow->id;");
        echo("var toggleC = 'toggletable'+$qrow->id;");
        ?>

        jQuery('.'+toggleC).click(function() {
        jQuery('.'+answersC).slideToggle('fast');
        jQuery('.'+newanswersC).slideToggle('fast');
        });
    });
        // -->
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:44:46+00:00Added an answer on May 26, 2026 at 6:44 pm

    instead of

    jQuery('toggleC').click(function() {
          jQuery('answersC').slideToggle('fast');
    

    try

    jQuery('.'+toggleC).click(function() {
          jQuery('.'+answersC).slideToggle('fast');
    

    note that if you have several toggles and answers on your page, a toggleC click with tooggle all answers. To toggle only answers belonging to the click answer use:

    jQuery('.'+toggleC).click(function() {
          jQuery(this).find('.'+answersC).slideToggle('fast');
    

    finally, note that you don’t need to add the the id in the class (transversability, yada yada), and that you can use something like

    jQuery('table').delegate('.'+toggleC, 'click', (function() {
        jQuery(this).find('.'+answersC).slideToggle('fast');
    

    in order to create only one event handler instead of “number of rows”. (but this ain’t very important)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First the script: <script type=text/javascript language=javascript> <!-- <?php echo(var answersC = 'answertable'+$qrow->id;); echo(var toggleC
i have this jquery $('.delete_step').live('click', function(e) { e.preventDefault(); var delete_location = 'http//<?php echo $_SERVER[SERVER_NAME];
This my script: <script type=text/javascript> var auto_refresh = setInterval( function () { $('#alert_div').load('/check_messages.php?timestamp=<?php echo
$(document).ready(function() { var user_id = document.getElementById('sessuid').value; //alert(user_id); //$('a.jtebizcard').attr('rel','<?php echo base_url();?>gl_profile/gl_getbig').cluetip({ $('a.jtebizcard').cluetip({ //alert(user_id); local: true,
$.post('includes/script.php', $(this).serialize(), function(data) { $('body').append(data); }); alert ($('#new').length) php script is <php echo <div
var loc_name = document.forms['create_<?php echo(htmlspecialchars($window_ID)); ?>'].elements['location_name']; alert(loc_name); This just gives me the message 'undefined'
The following does not work: var js_str= '<?php echo $str_from_server; ?>'; The problem is
How can we use PHP code in JavaScript? Like function jst() { var i
I'm trying to redo this in jQuery: <script language=JavaScript type=text/javascript> var thisIsSelected = '<?php
I'm struggling to determine why the addition of a new jQuery click function is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.