How to access DOM elements when the ids were dynamically created with numbers
I am building a forum page that submits a number of comments
with respect to a subject
much like the one we are on. Hence, when I acess the database to display the comments, I need to dynamically create the id of some div
's of my content, something like this:
while($row=mysql_fetch_assoc($result))
{
?>
<div id="post<?=$row["postId"] ?>" class="seeForumPost"><?=$row["post"] ?> </div> //this one
<div id="postFooter<?=$row["postId"] ?>" class="seeForumPostFooter"> //this one
<div class="byUser">
by <a class="commonLink" href="<?php print "seeUser.php?userId=".$row['userId'] ?>"> <strong><?=ucwords($row["firstname"]) ?> <?=ucwords($row["lastname"]) ?></strong></a>
</div>
<div class="likeThisForum"> Like this Post</div>
<div class="numberOfLikes"&开发者_运维问答gt; (<?=$row["postLikes"] ?> likes) </div>
<div id="<?=$row["postId"] ?>" onclick="reportPostSpam()" class="markAsSpam">Mark as spam</div> //this one
</div>
<?php } ?>
Thats right. I am using numbers inside my id, so they appears like "post26"
"postFooter26"
and "26"
But I don't know how to fetch these DOM objects in my ajax request (prototype). I cant't seem to deal with those parentNode
and previousSiblings
properties because then the javascript method throws cannot call property style of undefined
. Helps are appreciated.
You can always try:
<div id="<?=$row["postId"] ?>" onclick="reportPostSpam(<?=$row["postId"] ?>)" class="markAsSpam">Mark as spam</div>
Would be much simplier
精彩评论