0

I have a while statement that is echoing about 10 different stories. Every story has a user comment. When clicking the button edit I want one of the functions to hide the user comment. Here is the code I wrote to try and make it happen. Let me know if you think of why this wouldn't work.

<script>
    $(".edit_<?php echo $story_id ?>").click(function () {
    $(".comment_<?php echo $story_id ?>").hide("fast");
     });    
</script>
<?php
if (!empty($user_comment))
echo "
<p class='user_comment comment_$story_id'><strong>$user_comment</strong> <a class='edit_story_comment edit_$story_id'>Edit</a></p>";
else
echo "<p id='user_comment_$story_id'><span id='edit_no_story_comment'>Edit</span></p>";
?>
5
  • 1
    can you provide any more info? like whats happening or errors that are occuring, firebug errors etc Commented May 20, 2011 at 4:45
  • As a side note, the way you're implementing your jQuery is very inefficient and is going to go murderously slow on old browsers, as it will have to do a full DOM traversal to attach each event, and then another one every time the event is triggered. There are several things to do to fix this, but the most simple is just to assign unique IDs instead of unique classes and look up using $('#some-unique-id') instead of $('.some_class_name'), as ID based lookups are fast. Further improvements - use one handler for all with delegate/live and cache the lookup inside the callback. Commented May 20, 2011 at 4:49
  • It just wasn't hiding the user_comment. However the solution below fixed it. Commented May 20, 2011 at 4:50
  • El Yobo, I would have used ID's however you cannot assign multiple id's like you can with class. Example you can have class="class1 class2" but you cannot have id="id1 id2" - As far as my knowledge is concerned. Commented May 20, 2011 at 4:52
  • Great news your problems have been fixed! Commented May 20, 2011 at 4:52

1 Answer 1

2

so im gonna ges its becuse of the ankertag is being defined after you are hocking upp the event with jquery

use jquerys on load event to get around this

$(function() {
 $(".edit_<?php echo $story_id ?>").click(function () {
  $(".comment_<?php echo $story_id ?>").hide("fast");
 });    
});

hope this help

(mixing php html and jquery and string concatenation is beautiful btw :D)

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

Comments

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.