1

This is the toggle function without using a php variable as the div ID. The div ID that will be getting toggled is div3 in the code below.

<script>
 function showFormcaster() { 
       $('#div3').toggle();
    }
</script>

Because I am making a status system with many replicated divs I need the div id to be unique for each div that contains the reply box and submit button. I have decided to use the id from the status that will be getting replied to as the div id that will get toggled.

This is the div that will get toggled

$mail .= '<div id="'.$status_id.'?>" class="replyholder">';

below are a few methods I have tried to get this to work

METHOD1:

<script>
var show;
show = "'.$status_id.'";

     function showreply() { 
       $(show).toggle();
    }

METHOD 2:

<script>
 function showreply() { 
       $('#<?php echo $status_id ?>').toggle();
    }
</script>

METHOD 3

<script>
 function showreply() { 
       $('#<?php $status_id ?>').toggle();
    }
</script>

I am not so good at Javascript and most of the time when I come to using it, it is guess work and Google however I can't seem to find an answer and what I have found I have tried.

Is it even possible to do what I am trying to do and if so do you think you will be able to help me out and solve this.

Thank you.

8
  • How many of those replyholder divs will you have on the page? Commented Feb 14, 2014 at 15:09
  • It will depend on how many statuses the user has loaded on there screen. To start with I will probably need about 10 but as I will be adding the load more content js feature when a user hits the bottom of the page there will be another 10 needed and so on until the user stops scrolling down or the statuses runs out Commented Feb 14, 2014 at 15:18
  • Okay, so how is showreply() called? Commented Feb 14, 2014 at 15:20
  • $mail .= '<button class="replybutton" onclick="showreply();">Open Reply</button>'; That is how I call showreply() Commented Feb 14, 2014 at 15:28
  • And that always toggles a single replyholder div? Commented Feb 14, 2014 at 15:32

1 Answer 1

1

In html use:

<div id="<?= $status_id ?>" class="replyholder">THIS IS THE CONTENT</div>

Modify method 1 to this

show = '<?= $status_id ?>';
$('#' + show).toggle();

and pay attention to # symbol at

$('#' + show).toggle();

Method 2 looks ok for me, and in method 3 you forgot "print" command in php code

Sign up to request clarification or add additional context in comments.

7 Comments

I have tried what you have suggested and I still cannot seem to get it to work. doesn't do anything. I do appologize but i forgot to add the div into the $mail php variable I have now resorted out my original post with the php $mail variable the code with the div inside is this $mail .= '<div id="'.$status_id.'" class="replyholder">';
I deleted the comment I added as I included the wrong $mail I have now added the correct one
Solution is correct in general. Did you debug? Take a look at generated page source. And use FireBug to debug what do you have in "show" variable. And so on...
ok thank you. I will check it all out then as long as I know it is something going on this end and not within the js script then I should be able to solve the problem. Thank you for your time in answering.
I have just downloaded firebug and just used it to check. by the looks of it the <?php $status_id ?> within $('#<?php $status_id ?>').toggle(); doesn't seem to process the php variable and comes back as function showreply() { $('#').toggle(); } The reply system works fine using the $status_id variable and I am also include the js script after I am creating the $status_id
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.