JS tooltip works in a wrong way - javascript

My PHP:
<?php
foreach ($query as $row){
echo '<tr>
<td>
<label class="checkbox">
'.form_checkbox('delete[]', $row['link']).anchor("site/see_art/".$row['article_id'],$row['title']).'
<div class="tooltip-inner">'
.$row['description'].
'</div>
</td>
<td>'
.substr($row['pub_date'], 5, 12).
My jQuery:
<script>
$(".checkbox").mouseenter(function(){
var $tooltip = $('.tooltip-inner', this);
clearTimeout($tooltip.data('timeoutId'));
$tooltip.show(200);
}).mouseleave(function(){
var $tooltip = $('.tooltip-inner', this),
timeoutId = setTimeout(function(){
$tooltip.hide(200);
}, 650);
$tooltip.data('timeoutId', timeoutId);
});
$(".tooltip-inner").mouseenter(function(){
clearTimeout($(this).data('timeoutId'));
}).mouseleave(function(){
var $tooltip = $(this),
timeoutId = setTimeout(function(){
$tooltip.hide(200);
}, 650);
$tooltip.data('timeoutId', timeoutId);
});
</script>
'
</label>
</td>
</tr>';
}?>
In the PHP, I have some titles that display their description when I hover over them. Everything works fine except when I load the page the. When the page loads, the tooltips are visible until I hover over the link. After I hover over the link, the tooltips hide like they should. I know this happens because I echo the div. Some help?

You are not hiding your tooltips when you load the page.
$(document).ready(function(){
$('.tooltip-inner').hide();
});
If you already have a $(document).ready() then just put the hide code at the very top.

if you don't want them to flicker on then off use css to hide them instead of js.
so something like this in your css
.tooltip-inner {
display:none;
}

Related

Javascript add class active when click a link after load another page?

I have a list a.href to many URL contain film.
When I click a.href, it will reload my page. I don't know how to add the class .active to the a.href current selected.
In Ajax call, it simple like this code, it's working because the page is not reloaded. But I write on PHP and using $_GET and $_POST method. So the page must be reloaded.
Have any method to add the class .active on a.href link was clicked?
<div class="btn-group listEp col-md-12">
$i = 0;
Episode:
<?php foreach($data['episodes'] as $epList) { ?>
<a class="btn btn-default" href="<?php echo $epList['href']); ?>"> <?php echo $i++; ?> </a>
<?php } ?>
</div>
$(function() {
$('#listEp').on("click", "a", function() {
$( "#listEp a:first-child" ).css("cssText", "background: #4CAF50 !important;");
$(this).addClass('active').siblings().removeClass('active');
});
});
If you don't want to reload page after click and create a custom action on this just do this:
$('#listEp').on("click", "a", function(e) {
e.preventDefault() // this will disable default action which is redirect in case of a element
e.stopPropagation() // this will stop element beeing propagated by other functions
$( "#listEp a:first-child" ).css("cssText", "background: #4CAF50 !important;");
$(this).addClass('active').siblings().removeClass('active');
});
Looks like you're targeting an ID which doesn't exist, the element has a class listEp. Try $('.listEp') instead
You can do simply like this,
$('a.btn[href]').click(function(){
$('a.btn[href]').removeClass('active');
$(this).addClass('active');
});
above code removes active class from other same elements and adds class in this element.
Add below code to that page which is rendering on reload,
$(document).ready(function(){
$('a.active.btn[href]').removeClass('active');
});

Calculate height according to div's inner text

I've got a PHP loop to create multiple divs which display data from a database.
<?php
foreach($result as $row) {
?>
<div class="container-fluid" id="content-fluid">
<div class="container-update">
<div class="update-group">
<div class="update-header">
<?php echo $row['update_title'] ?>
<div class="update-author">
by <?php echo $row['update_creator'] ?>
</div>
</div>
<div class="update-body">
<?php echo $row['update_text']; ?>
</div>
</div>
</div>
</div>
<?php
}
?>
I made them clickable through jQuery and they do increase in height:
$(document).ready(function() {
$('.update-group').click(function() {
var clicks = $(this).data('clicks');
if(clicks) {
$(this).removeAttr('style');
}
else {
$(this).animate({height: '200px'}, 300);
}
$(this).data("clicks", !clicks);
});
});
Default height is set to a specific value to ensure that all divs are of the same height and overflow is set to hidden. Clicking on them should expand them downwards according to the text inside of it.
Divs of which the text fits just fine, shouldn't be able to expand at all. And divs with an excessive amount of text should expand to a calculated height.
I've set up a jsfiddle program to demonstrate what I mean: https://jsfiddle.net/vz6s9brd/
Right now it just animates to 200px and back. I think you're looking for something like this:
$('.update-group').click(function() {
var clicks = $(this).data('clicks');
if(clicks) {
$(this).removeAttr('style');
}
else {
var bod = $(this).find('.update-body');
var head = $(this).find('.update-header');
var neededHeight = bod.height() + head.height();
if ($(this).height() < neededHeight) {
$(this).animate({height: neededHeight + 'px'}, 300);
}
}
$(this).data("clicks", !clicks);
});
Rather than calculating the height, you can instead change your overflow settings. You're wanting to toggle between two modes - one with limited height and no overflow, and the other with height determined by the content.
if(clicks) {
$(this).css({overflow: 'hidden', height: '6em'});
}
else {
$(this).css({overflow: 'inherit', height: '100%'});
}
Unfortunately, doing it this way doesn't animate very well. Even if you replace the css() calls above with calls to animate(), the result is unsatisfying. How important the animation is to you is up to you.

hiding divs using javascript that don't match the clicked div and pushing correct div up the page

I have a web page in PHP that shows blog posts/ articles that were posted from 2015-2008. basically at the top of the page I have buttons that have the year on it, and below all the blog posts and articles correlatin to the years. I want to have a feature so that if the user clicks 2013, all other posts with years not matching 2013 will dissapear and the correct year posts/articles will move up to the top of the page, so the user doesn't have to scroll.
Here is my code, the content is loaded through a controller so the page is basically created by grabbing data from an array.
<div class="calendar-key">
<div class="cd-timeline2-img cd-2015" id="btn" >'15</div>
<div class="cd-timeline2-img cd-2014" >'14</div>
<div class="cd-timeline2-img cd-2013" >'13</div>
<div class="cd-timeline2-img cd-2012">'12</div>
<div class="cd-timeline2-img cd-2011" >'11</div>
<div class="cd-timeline2-img cd-2010" >'10</div>
<div class="cd-timeline2-img cd-2009" >'09</div>
<div class="cd-timeline2-img cd-2008" >'08</div>
<div class="cd-timeline2-img cd-2007" >'07</div>
<?php foreach ($article as $slug => $article): ?>
<div class="cd-timeline-block">
<div class="cd-timeline-img cd-<?php echo $article['year'] ?>">
<span class="timelinedate"><?php echo $article['month'] ?><?php echo $article['day'] ?></span>
</div>
<div class="cd-timeline-content">
<a href="<?php echo $article['link'] ?>">
<h3 class="press_title" id="<?php echo $article['year'] ?>-<?php echo $article['first'] ?>"><?php echo $article['title'] ?></h3>
<img src="<?php echo $article['image'] ?>" width="100%" height="auto" />
</a>
<p><?php echo $article['description'] ?> </p>
<div class="social-share-buttons article-share-buttons pull-left">
<a class='social-email' href='<?php echo SocialShareLink::email("", "Check out this article, ".$article['title']." \n\n" .$article['link']); ?>'</a>
<a class='social-facebook' href='<?php echo SocialShareLink::facebook($article['link']); ?>' target='_blank'></a>
<a class='social-twitter' href='<?php echo SocialShareLink::twitter("", $article['link']); ?>' target='_blank'></a>
<a class='social-google' href='<?php echo SocialShareLink::googleplus($article['link']); ?>' target='_blank'></a>
</div>
Read more
</div>
</div>
<?php endforeach; ?>
the for each basically takes an array I have in a config file with data, and populates the articles on the page for however many there are in the array.
'articles' => [
'article#1 example' => [
'description' => "exampletextblablabla",
'title' => 'article title',
'link' => '//www.google.com',
'year' => '2015',
'month' => 'Jul ',
'day' => '25',
'first' => '1', //first article of that year
'image' => '../../images/example.jpg'
],
//more articles in this format
and then my failed attempt at js
<script type="javascript">
$('.cd-timeline2-img a').on('click',function(){
var eq = $(this).index();
$('cd-timeline-img cd-<?php echo $article['year'] ?>').removeClass('show');
$('cd-timeline-img cd-<?php echo $article['year'] ?>').eq(eq).addClass('show');
});
</script>
I am not very good at javascript, so I tried something like this but nothing works. I feel like this is an easy little script to right, any help would be great! thanks.
Woohoo! Finally, I got it working and 100% where I want it. So I used your code and threw it into a Fiddle: https://jsfiddle.net/rockmandew/4Lyofmkh/44/
You will see that I went with my approach - by default, the articles are display none and have a class of 'show' on them to begin with - you will also notice I changed where you had your 'cd-(year)' - I did this because you needed to identify the entire article container since that is what you would be hiding/showing.
I also added a 'Show All' button, so the user can view all if desired.
So like I said I changed your HTML markup right here:
<div class="cd-timeline-block cd-2015 show">
<!-- Article Year in Div Class below -->
<div class="cd-timeline-img">
It was "cd-timeline-img cd-2015" previously. That is the only change you need to make to your markup, besides adding the "show all" button (if you want it):
<div class="show-all">Show All</div>
Furthermore, I applied the display:none css property to the ".cd-timeline-block"
.cd-timeline-block {
margin-top:35px;
display:none;
}
Which is why we initialize the page with the "show" class on there:
<div class="cd-timeline-block cd-2015 show">
Which has the following styles:
.show {
display:block;
}
Finally, we get to the meat of it all, the jQuery. I will post the working code that I used and then explain it. The following code is to toggle articles based on the year clicked:
$('.calendar-key a').each(function() {
$(this).on('click', function() {
var x = 'cd-' + $(this).attr('rel');
$('.cd-timeline-block').each(function() {
if($(this).hasClass(x)) {
$('.cd-timeline-block').not(this).each(function() {
if($(this).hasClass('show')) {
$(this).toggleClass('show');
}
});
$(this).each(function() {
if(!$(this).hasClass('show')) {
$(this).toggleClass('show');
}
});
}
});
})
});
As you will see, when a ".calendar-key" link is clicked, the clicked link will produce a variable based on the links "rel" - the variable adds the "cd-" prefix onto the "rel" value. Then for each ".cd-timeline-block" if it has the class that was created from clicking (the variable just discussed), it will cycle through all of the ".cd-timeline-block" elements that isn't "this" (meaning all elements that don't match the selected year.) - for all of those elements, if it has the "show" class, it will be toggled. Then the last part, it takes the "this" and cycles through each of them, if it doesn't have the class "show", then it toggles the class "show", thus displaying the desired elements.
Finally, the show all button is controlled with the following function:
$('.show-all a').on('click', function() {
$('.cd-timeline-block').each(function() {
if(!$(this).hasClass('show')) {
$(this).toggleClass('show');
}
});
});
Its a fairly simple function. When the ".show-all" link is clicked, it cycles through all of the ".cd-timeline-block" elements, again, if they don't have the "show" class, the function will toggle the "show" class.
I know that was a lot but hopefully it all made sense. Again, here is the associated Fiddle I made to help you.
https://jsfiddle.net/rockmandew/4Lyofmkh/44/
Let me know if you need any further help.
Latest Update:
https://jsfiddle.net/rockmandew/4Lyofmkh/46/
Fiddle now contains new mark-up for easiest solution to updated issue marked in comments below.
It finally dawned on me that I was filtering your list of years not your posts. For future reference post a snippet of actual code, now bits with php all over them. Now the script looks for the rel attribute in your year list for the year, matches that year to the class "cd-year" in each post and filters as necessary. I stripped out all of the superfluous parts of the post like the social links as they're not needed for this exercise.
http://jsfiddle.net/1dyocexe/2/
//this is where you'd have your <?php echo $article['year'] ?> set the current year
var year = 2014;
getPosts(year);
function getPosts(year) {
$(".cd-timeline-block").find("div").each(function (i) {
var elm = $(this);
var cdYear = "cd-" + year;
var use = elm.hasClass(cdYear);
if (use === true) {
elm.show();
} else {
elm.hide();
}
});
}
$(".calendar-key div a").on("click", function () {
var year = $(this).attr("rel");
getPosts(year);
});

Hide/show div with javascript and while loop

I am trying for hours now and for some reason i can't find a solution for my problem.
The thing is, i need something like a div thats showing up when i click on an link.
That's working so far.
But the main problem is, that i need it work within a while loop. So i can get the data dynamically into grid, but all of the php created links have the same id and all of the "to-show-divs" show up at the same time. so my question is how can i create dynamic ids or classes and how do i get them to work within javascript?
echo "<div class='grid grid-pad'>";
$db=mysql_query("SELECT * FROM work") or die (mysql_error());
while($var=mysql_fetch_array($db))
{
echo "<div class='col-1-3'>
<div class='content'>
<div id='thumb' ><img alt='$var[id]' src='$var[thumb]'/></div>
<div class='menu' style='display: none;'>$var[link]</div>
</div>
</div>";
}
echo "</div>";
<script>
$(document).ready(function() {
$('#thumb').click(function() {
$('.menu').slideToggle("fast");
});
});
</script>
You can do what the comment suggests or just make an iterator variable and concat that to the id like so:
$index = 0;
while($var=mysql_fetch_array($db))
{
echo "<div class='col-1-3' id='item_". $index ."'>
<div class='content'>
<div id='thumb_". $index++ ."' >STUFF</div>
<div class='menu' style='display: none;'>$var[link]</div>
</div>
</div>";
}
Change <div id="thumb"> to <div class="thumb"> and then use following jQuery:
$('.thumb').click(function() {
$(this).next('.menu').slideToggle("fast");
});
Otherwise you'll have multiple elements with the ID "thumb", which you shouldn't.
Since the DIV is right after the link, you can use the JQuery function next() to get the next node after the link, which is the DIV:
$(document).ready(function() {
$('#thumb').on('click', function(e) {
// prevent IE from actually executing the href of the link
e.preventDefault();
// get next node, call the toggle.
$(this).next().slideToggle("fast");
// prevent executing of the href of the link
return false;
});
});
If you have more of these "thumb" links, please us a class instead of an ID - an ID has to be unique.
Edit:
Realizing that there actually are no links but DIVs, you can skip the "prevent executing" stuff of course.

jquery and jscript if else

I'm using the code below to make a box appear when certain text is clicked. How do i limit it so only one box can be slid down at a time.
E.g. if i click the first button it slides down, then if i click the second button the first box slides up again?
Here's my code
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip1").click(function(){
$(".panel1").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip2").click(function(){
$(".panel2").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip3").click(function(){
$(".panel3").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip4").click(function(){
$(".panel4").slideDown("fast");
});
});
</script>
<div id="betslip">
<td><div class="panel1"><?php echo $bet1; ?></div></td>
<td><div class="panel2"><?php echo $bet2; ?></div></td>
<td><div class="panel3"><?php echo $bet3; ?></div></td>
<td><div class="panel4"><?php echo $bet4; ?></div></td>
</div>
Well, you could probably start by adding in some code to .slideUp() each of the other panels. Have you tried that yet?
You can refactor and minimize your code like this:
$(document).ready(function(){
$("[class=^'flip'").click(function(){
// get panel class
var panelClass = this.className.replace('flip', 'panel');
// hide all open
$("[class^='panel']").hide();
// show current one now
$('.' + panelClass).slideDown('fast');
});
});
That's all you need.
Give all the flips a common class name:flip:
$('.flip').click(function(){
$('.panel').slideUp('fast'):
});
This will SlideUp all the panels, then slide down with the functions you have:
$(document).ready(function(){
$(".flip1").click(function(){
$(".panel1").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip2").click(function(){
$(".panel2").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip3").click(function(){
$(".panel3").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip4").click(function(){
$(".panel4").slideDown("fast");
});
});
$(document).ready(function(){
$(".flip1").click(function(){
$("div[class^=panel]").slideup();
$(".panel1").slideDown("fast");
});
$(".flip2").click(function(){
$("div[class^=panel]").slideup();
$(".panel1").slideDown("fast");
});
// Do the rest
});
Use just one document.ready()
$(document).ready(function(){
$('[class^="flip"]').click(function(){
var thisId = $(this).attr('class').replace('flip');
$('[class^="panel"]').slideUp("fast");
$('.panel'+thisId).slideDown("fast");
});
});
Note that this would only work if you have only one class assigned for .panel and .flip elements

Categories

Resources