// Here based on the class rply1 I am displaying replies
<div class="biz_gary_btn width80 pl5 fl rply1<?php echo $comm['comment_id'];?>">
<div class="fl pt5_ie pt1"><img src="<?php echo base_url();?>images/green_callot.png" alt="Green Callot"></div>
<div class="arial bold fnt11 c7b7b7b fl pl5 ">2 Replies </div>
</div>
<!--2 Replies-->
<!--Reply-->
<div class="biz_gary_btn width84 pl5 fl rply2<?php echo $comm['comment_id'];?>">
<div class="fl pt5_ie pt1 pl5"><img src="<?php echo base_url();?>images/light_blue_callot.png" alt="Blue Callot"></div>
<div class="arial bold fnt11 c7b7b7b fl pl7">Reply</div>
</div>
This is the script
<script type="text/javascript">
$(document).ready(function(){
$(".rply1<?php echo $comm['comment_id'];?>").click(function(){
alert("sdfsd");
//return true;
$("#2replies<?php echo $comm['comment_id'];?>").toggle();
$(".rply1<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
$(".rply2<?php echo $comm['comment_id'];?>").click(function(){
$("#reply<?php echo $comm['comment_id'];?>").toggle();
$(".rply2<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
});
</script>
But the function is being called twice. I can see the alert twice. After the first alert my #2replies div is visible. This working just fine in the HTML set-up.
This might not be ideal because without seeing your full code it is hard to tell if you are actually binding the event twice. But, instead you could ensure that a click event is unbound before binding it again:
<script type="text/javascript">
$(document).ready(function(){
$(".rply1<?php echo $comm['comment_id'];?>").unbind("click").bind("click", function(){
alert("sdfsd");
//return true;
$("#2replies<?php echo $comm['comment_id'];?>").toggle();
$(".rply1<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
$(".rply2<?php echo $comm['comment_id'];?>").unbind("click").bind("click", function(){
$("#reply<?php echo $comm['comment_id'];?>").toggle();
$(".rply2<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
});
</script>
Related
I have one index.php page, whose content changes whether you activated a session or are logged out.
When users are logged in, a click on the link "showhide" should show the class "users".
When users are logged out the link "showhide" is hidden and instead the link "showhide2" is visible, which should show the class "users2" when its pressed.
I found a jQuery-snippet online that does exactly this and works fine for the link "showhide"
Unfortunately it doesn't work for the link "showhide2" - the class is always visible...
Here's my code:
<div id="content" style="margin-top:10px;height:100%;">
<?php
/*echo "Der Nutzername ist ".$_SESSION['user'];
echo "<br>Die Session lautet ".$_SESSION['sessionname'];
echo "<br>".session_id();*/
$sArray = explode(".",$_SESSION['sessionname']);
$session1 = $sArray[0];
$session2 = $sArray[1];
$sessionausblenden = $_SESSION['sessionname'];
if (!isset($sessionausblenden)){
echo "<style type='text/css'>
#showhide{
visibility:hidden !important;
}
.users{
visibility:hidden !important;
}
#logout {
visibility:hidden !important;
}
</style>";
}
elseif (isset($sessionausblenden)){
echo "<style type='text/css'>
#showhide2{
visibility:hidden !important;
}
.users2{
visibility:hidden !important;
}
</style>";
}
?>
<a id="showhide" href="#" style="background:#<?php echo $session2?>;">+</a>
<a id="showhide2" href="#" style="background:#<?php echo $session2?>;">?</a>
<a id="logout" onclick="return confirm('Are you sure?')" href="logout.php">-</a>
<script type="text/javascript">
$(document).ready(function () {
$('.users').hide();
$('a#showhide').click(function () {
$('.users').toggle(400);
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$('.users2').hide();
$('a#showhide2').click(function () {
$('.users2').toggle(400);
});
});
</script>
<center><h1>Group Chat In PHP</h1></center>
<div class="chat">
<div class="users" style="background-color:#<?php echo $session2?>;">
<?php include("users.php");?>
</div>
<div class="users2" style="background-color:#<?php echo $session2?>;">
<?php include("users.php");?>
</div>
<div class="chatbox">
<?php
if(isset($_SESSION['user'])){
include("chatbox.php");
}else{
$display_case=true;
include("login.php");
}
?>
</div>
</div>
</div>
You can see the live-version here: http://team3.digital-cultures.net/index.php
Just enter a name of your choice and choose an option from "Start" and "Ziel" and your session is started (and a chat is opened).
Can you help me finding the mistake?
Thanks!
It seems an event is not being bound to #showhide2. If you listen at the document level and let it bubble to #showhide as the target, it may work. This can happen for dynamically loaded elements.
Change
$(document).ready(function () {
$('.users2').hide();
$('a#showhide2').click(function () {
$('.users2').toggle(400);
});
});
To
$(document).ready(function () {
$('.users2').hide();
$(document).on('click','#showhide2',function () {
$('.users2').toggle(400);
});
});
Most importantly, please resolve this error:
$(".msgs").animate({scrollTop:$(".msgs")[0].scrollHeight}); on line 2 of chat.js $(".msgs")[0] is undefined.
There are no elements matching $('.msgs'). Wrap it with the following:
if($('.msgs').length) {
$(".msgs").animate({scrollTop:$(".msgs")[0].scrollHeight});
}
This maybe why the event isn't getting bound.
Hi I'm doing a chat script that I've gotten somewhere that needs quite modifications. The function I need to achieve is after successful loading of user to chat with the view should go to bottom directly. I have done things which failed on several occasions, it does automate to bottom but when I tried scrolling the content it still goes back to bottom which should not happen.
This is what I have so far.
<?php
$ctr = 0;
if (is_array($buddies)) {
foreach ($buddies as $buddy) {
?>
<span style="float: right;"><?php echo $buddy->unreadMsgCtr; ?></span>
<b><?php echo humanize($buddy->fullName) ?></b> <br />
<?php
++$ctr;
}
}
?>
<script type="text/javascript">
$(document).ready(function() {
<?php for ($ctrjs = 0; $ctrjs < $ctr; $ctrjs++) {?>
$('#friend' + <?php echo $ctrjs; ?>).live('click', function() {
$('#buddies').hide();
var chattingWith = $(this).attr('chattingID');
startrefresh();
$('#chatroom').show();
$('#chatmessage').html('<img src="<?php echo site_url('/images/ajax-loader.gif');?>" width="16" height="16" />');
$.post("<?php echo site_url('chats/chatbuddy');?>",{
username: $("#friend"+<?php echo $ctrjs; ?>).html(),
recipientID: chattingWith
});
});
<?php } ?>
});
</script>
//chats.php when a particular buddy has been clicked.
<script type="text/javascript">
var autoScrollEnabled = true;
$(document).ready(function() {
toggleAutoScroll();
if(autoScrollEnabled == true) {
$(".messages").animate({ scrollTop: $('.messages')[0].scrollHeight}, 1000);
//return false;
toggleAutoScroll();
}
function toggleAutoScroll() {
autoScrollEnabled = !autoScrollEnabled; //! reverses the value, true becomes false, false becomes true
if (autoScrollEnabled == true) { //scroll down immediately if a.s. is reenabled
$(".messages").animate({ scrollTop: $('.messages')[0].scrollHeight}, 9999999999);
}
}
$('#closechat').live('click', function() {
$('#chatroom').hide();
//scrolled = 0;
stoprefresh();
});
});
</script>
<div class="headerchat" style="width: 217px;"><div class="buddycontainer"> Chatting</div>
<div class="closecontainer">✖ </div>
<br class="breaker" />
</div>
<?php
if (is_array($chat)) {
foreach ($chat as $item){ ?>
<div class="conversation" style="width: 215px;">
<b><span class="username"><?php echo $item['username']; ?></span></b>
<span class="gray" style="float: right;" title="<?php echo date('M d, Y g:i A', $item['time']);?>"> <?php echo date('g:i A', $item['time']);?></span>
<br /><?php echo $item['message']; ?>
</div>
<?php } ?>
<?php }?>
BTW here is the jsfiddle I don't understand it's working properly in jsfiddle. http://jsfiddle.net/STQnH/3/
I'm not sure if I am heading on the right track. I just want to achieve that. Thank you guys.
Try wrapping your code in:
$(document).ready(function(){ //your code here
});
or
$( window ).load(function() {//your code here
});
If it works in jsFiddle but not on the live site this is likely what's missing. jsFiddle adds it for you. You can change this option in the drop down menu indicated by my freehand circle.
jQuery faq for $(document).ready()
i am creating a comment system using the php and mysql to store data and ajax with jquery all the system work well but when it comes to the delete action nothing it happen like this button do not have any action or relation with the system can anyone help me ???
comment_box.php
<li class="comment-holder" id="_<?php echo $comment->comment_id; ?>">
<div class="user-img">
<img src="<?php echo $user->profile_img; ?>" class="user-img-pic" />
</div>
<div class="comment-body">
<h3 class="username-field">
<?php echo $user->userName; ?>
</h3>
<div class="comment-text">
<?php echo $comment->comment; ?>
</div>
</div>
<div class="comment-buttons-holder">
<ul>
<li id="<?php echo $comment->comment_id; ?>"class="delete-btn">X</li>
</ul>
</div>
</li>
comment_delete.js(i am testing the delete button with firebug)
$(document).ready(function() {
$('.delete-btn').each(function() {
var btn = this;
$(btn).click(function(){
console.log("the id " + btn.id);
})
});
});
Try this
$(document).ready(function() {
$('.delete-btn').each(function(i,el) {
$(el).click(function(){
console.log("the id " + $(this).attr('id'));
})
});
});
<li id="<?php echo $comment->comment_id; ?>"class="delete-btn">X</li>
Since you are missing whitespace here between the id value and the class attribute, I think the browser just does not assign that class to the element.
Edit (for people who don’t know what “missing” means):
Your PHP code will generate HTML output of the form
<li id="123"class="delete-btn">X</li>
whereas it should of course be
<li id="123" class="delete-btn">X</li>
Please always validate your HTML code before asking questions.
Try
$(document).ready(function() {
$('.delete-btn').click(function() {
var comment_id = $(this).attr('id');
alert(comment_id);
});
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script src="<?php $this->baseUrl()?>/public/js/jQuery.bubbletip-1.0.6.js" type="text/javascript"></script>
<link href="<?php $this->baseUrl()?>/public/js/bubbletip/bubbletip.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function() {
for(i=1;i<12;i++){
$('#a'+i).bubbletip($('#tip'+i), { deltaDirection: 'right' });
}
});
</script>
code in header part
<?php
foreach($this->nominations as $nomination)
{
for($i=1;$i<12;$i++){
if($nomination['award'.$i]!=""){?>
<div id="tip<?php echo $i?>" style="display:none;">
<div class="star"><strong><?php echo $nomination['award'.$i];?></strong></div>
<div><strong>Project: </strong><?php echo $nomination['project'.$i]?></div>
</div>
<?php }}
for($i=1;$i<12;$i++){
if($nomination['award'.$i]!=""){
echo "<span id='a$i'>";
echo "<img src='/public/assets/images/icons/star.png'/>";
echo "</span>";
}}
}?>
code in body section
My problem is when take my mouse over the stars of first iteration of foreach everything is working fine but its not working from second iteration i found that problem is with id a,tip becuause they are always becoming a1,a2.. and tip1,tip2... is there any solution
it is because every iteration of your forach loop creating elements of same id from a1 t a12
you need to put another level in names of ids .Try to use following
<?php
$count=0;
foreach($this->nominations as $nomination)
{
$count++;
for($i=1;$i<12;$i++){
if($nomination['award'.$i]!=""){?>
<div id="tip<?php echo $i?>_<?php echo $count?>" style="display:none;">
<div class="star"><strong><?php echo $nomination['award'.$i];?></strong></div>
<div><strong>Project: </strong><?php echo $nomination['project'.$i]?></div>
</div>
<?php }}
for($i=1;$i<12;$i++){
if($nomination['award'.$i]!=""){
echo "<span id='a$i_$count'>";
echo "<img src='/public/assets/images/icons/star.png'/>";
echo "</span>";
}}
}
<input type="hidden" id="total_iteration" name="total_iteration" value="<?php echo $count?>"/>
?>
And change you javascript code accordingly
<script type="text/javascript">
$(document).ready(function() {
var total=$('#total_iteration').val();
var t=0;
for(t=1;t<total;t++)
{
for(i=1;i<12;i++){
$('#a'+i+'_t').bubbletip($('#tip'+i+'_'+t), { deltaDirection: 'right' });
}
}
});
</script>
After some debigging above code should run the way you want i haven't tested it but i guess the main problme is the elements of duplicate ids in each foreach iteration
Here is the JavaScript I use to animate slider (fade effect) of the content I read from database:
<script language="javascript">
jQuery(document).ready(function ()
{
var terms = ["span_1","span_2"];
var i = 0;
function rotateTerm() {
jQuery("#text-content").fadeOut(200, function() {
jQuery(this).text(jQuery('#text-slider .'+terms[i]).html()+i).fadeIn(200);
});
jQuery("#title-content").fadeOut(200, function() {
jQuery(this).text(jQuery('#title-slider .'+terms[i]).html()+i).fadeIn(200);
i == terms.length - 1 ? i=0 : i++;
});
}
rotateTerm();
setInterval(rotateTerm, 1000);
});
</script>
And here is the PHP code I use:
<?php
if (!empty($testLst)) :
$num=1;
foreach($testLst as $key=>$item):
$item->slug = $item->id;
$item->catslug = $item->catid ;
?><div id="hidden-content" style="display:none;">
<div id="title-slider">
<span class="<?php echo 'span_'.$num; ?>">
<h4><a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($item->id, $item->catid)); ?>">
<?php echo $item->title; ?></a>
</h4>
</span>
</div>
<div id="text-slider">
<span class="<?php echo 'span_'.$num; ?>">
<p>
<?php
$concat=array_slice(explode(' ',$item->introtext),0,20);
$concat=implode(' ',$concat);
echo $concat."...";
?>
</p>
</span>
</div></div>
Learn more >></p>
<?php
$num++;
endforeach;
endif;
?>
<div id="title-content">
</div>
<div id="text-content">
</div>
And here is a JSFiddle page reproducing what I would like to do.
My problem is that I am getting data that still has HTML tags, however I would like the output to have my CSS styles.
You could clone the node, and set that to be the new content of the target elements, to keep everything in jQuery objects, but personally, I'd use the .outerHTML property.
I've updated your fiddle to show you what I mean: I've changed the .text(...set content here) to .html(), because we're injecting HTML content. Then, I added [0] at the end of your selector, to return the raw element reference, which gives access to all standard JS properties and methods an element has, and just went ahead and fetched the outerHTML... easy-peasy