jQuery delay and get php variable - javascript

I have some problems with jQuery.
I have code that is running ok, but I want to add some delay (fade in, etc...) on this both functions.
$(document).ready(function(){
$(".product-item, .rade-test").mouseover(function(){
$('.just-to-define').removeClass('rade-test-div');
$('.just-to-define').addClass('rade-test-div2');
$('.heredefine').removeClass('rade-test');
$('.heredefine').addClass('rade-test2');
});
$( ".product-item, .rade-test" ).mouseout(function() {
$('.just-to-define').addClass('rade-test-div');
$('.just-to-define').removeClass('rade-test-div2');
$('.heredefine').addClass('rade-test');
$('.heredefine').removeClass('rade-test2');
});
});
I tried with
$(".product-item, .rade-test").mouseover(function(){
$('.just-to-define').removeClass('rade-test-div');
$('.just-to-define').addClass('rade-test-div2');
$('.heredefine').removeClass('rade-test');
$('.heredefine').addClass('rade-test2');
}, 2000);
but that's not working from some reason. I don't know why. So here when it have to add class i need fade in/delay/'process time', and i already tried with .fadeIn("slow")
Second what I have problem with this is, I have table foreached in my template, and when mouse is over table (see in jQuery function) it's opening every class that i have write (rade-test-div2 and rade-test2), and when I add .first() it's only first ofc. How can I open only table where is my mouse?
product-item class is in TR element of table, so I can add something like
#php $uniqueId = uniqId(); #endphp
<tr class="product-item {{$uniqueId}}">
But how to write in jQuery to select this $uniqueId? My jQuery code is in another file.

To make an animation, use a CSS "transition" property: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions. Then, when a CSS class will be added to an element, the animation will be triggered.
To manipulate the active element use "this", e.g.:
$(".product-item, .rade-test").mouseover(function(){
var $this = $(this);
$this.find('.just-to-define').removeClass('rade-test-div');
$this.find('.just-to-define').addClass('rade-test-div2');
$this.find('.heredefine').removeClass('rade-test');
$this.find('.heredefine').addClass('rade-test2');
}, 2000);
Example: https://jsfiddle.net/8vjmhztr/2/

Related

Using jquery to show and then hide div - memorygame

I'm trying to create a memory game using html ccs js and jquery.
The problem I'm having is that when I'm trying to show the div that states if the answer was correct, the div becomes visible like I want but it won't disappear.
I have tree conditions and the problem occurs with every one of them:
$(".wrong").show(function(){
$(this).hide(1000);
});
you can use setTimeout function to hide the element.
setTimeout(function () {
$(".wrong").hide()
}, 1000);

Javascript onclick event firing for id but not for class

I'm fairly new to Javascript, and am trying to get an 'on click enlarge' kind of effect, where clicking on the enlarged image reduces it again. The enlarging happens by replacing the thumbnail by the original image. I also want to get a slideshow using images from my database later on.
In order to do that, I made a test where I replace the id which indicates enlarging is possible by a class and I also use a global variable so that I can keep a track of the url I'm using. Not sure this is the best practice but I haven't found a better solution.
The first part works fine, my image gets changed no problem, values are also updated according to the 'alert' statement. However, the second part, the one with the class never triggers.
What am I doing wrong (apart from the very likely numerous bad practices) ?
If instead of changing the class I change the id directly (replacing .image_enlarged by #image_enlarged, etc.), it seems to call the first function, the one with the id, yet outputs the updated id, which is rather confusing.
var old_url = "";
$(function(){
$('#imageid').on('click', function ()
{
if($(this).attr('class')!='image_enlarged'){
old_url = $(this).attr('src');
var new_url = removeURLPart($(this).attr('src'));
$(this).attr('src',new_url); //image does enlarge
$(this).attr('class',"image_enlarged");
$(this).attr('id',"");
alert($(this).attr('class')); //returns updated class
}
});
$('.image_enlarged').on('click', function (){
alert(1); //never triggered
$(this).attr('src',old_url);
$(this).attr('class',"");
$(this).attr('id',"imageid");
});
});
function removeURLPart(e){
var tmp = e;
var tmp1 = tmp.replace('thumbnails/thumbnails_small/','');
var tmp2 = tmp1.replace('thumbnails/thumbnails_medium/','');
var tmp3 = tmp2.replace('thumbnails/thumbnails_large/','');
return tmp3;
}
As for the html, it's really simple :
<figure>
<img src = "http://localhost/Project/test/thumbnails/thumbnails_small/image.jpg" id="imageid" />
<figcaption>Test + Price thing</figcaption>
</figure>
<script>
document.write('<script src="js/jquery-1.11.1.min.js"><\/script>');
</script>
<script type="text/javascript" src="http://localhost/Project/js/onclickenlarge.js"></script>
From the API: http://api.jquery.com/on/
The .on() method attaches event handlers to the currently selected
set of elements in the jQuery object.
When you do $('.image_enlarged').on(...) there is no element with that class. Therefore, the function is not registered in any element.
If you want to do so, then you have to register the event after changing the class.
Here's an example based on your code: http://jsfiddle.net/8401mLf4/
But this registers the event multiple times (every time you click) and it would be wrong. So I would do something like:
$('#imageid').on('click', function () {
if (!$(this).hasClass('image_enlarged')) {
/* enlarge */
} else {
/* restore */
}
}
JSfiddle: http://jsfiddle.net/8401mLf4/2/
Try using:
addClass('image-enlarged')
instead of:
.attr('class',"image_enlarged");
the best way to do this would be to have a small-image class and a large image class that would contain the desired css for both and then use addClass() and removeClass depending on which you wanted to show.

using custom plugin on run-time created table jquery

I am creating a table run time in the memory and later appending it to dom.
Something like this :
var myTable = $('<table></table>').attr({ id: "dataMatrixtable" }).attr({class: "tipTable",cellpadding: "5",cellspacing: "0"});
myTable.appendTo(this.top.$("#tableContainer"));
This.top because table is in a iframe.
Now I wrote a plugin something like this.
(function($){
$.fn.extend({
mailServer : function() {
this.addClass("blah");
},
})
})(jQuery)
when i am calling this function mailServer after appending the table to dom
$("#dataMatrixtable").mailServer();
or
this.top.$("#dataMatrixtable").mailServer();
either way its not working (not appending the class)
Where as it works for the elements which is already in the dom when page loads..
How to solve this issue.please help
Actually I found a way its working now ..
What I did is
myTable.mailServer(); (its working and I am able to access the plugin funciton mailServer()
In place of doing
$("#dataMatrixtable").mailServer();

Change CSS class name dynamically

I need to change the CSS class name of all the elements in a page with a particular class name (.k-textbox). I tried the below code but it does not hit inside the .each() function
<script>
$(document).ready(function () {
$(".k-textbox").each(function () {
//alert("a");
$(this).removeClass("k-textbox");
$(this).addClass("input-medium");
});
});
</script>
In the page i have a 3rd party grid control. the CSS class i have mentioned is inside that third party grid control.
below is the DOM object:
You should try to use chaining API provided by jQuery library:
$(".k-textbox").removeClass("k-textbox").addClass("input-medium");
edit:
As long as the elements are created dynamically you could try to run this code after those elements are created. But if you don't know when they are inserted into the code and doesn't have control over them you could try write simple watch function, i.e:
var watchTimer = setInterval(function () {
var inputs = $('.k-textbox');
if (inputs.length) {
// clear interval
clearInterval(watchTimer);
// change class
inputs.removeClass("k-textbox").addClass("input-medium");
}
}, 100);
use addClass() and removeClass()
$(".k-textbox").removeClass("k-textbox").addClass("input-medium");

jQuery code critque

Thought I'd post here. My first hour on jQuery, actually first programing ever done. Would love to learn whats not right and how it could be better.
$(function() {
function hide_me()
//A place to specify which elements you want hidden, on page load.
{
$("li.credentials").hide();
}
function first_bow()
//The div right_column takes a bow on initial load.
{
$("div#right-column").show("drop");
}
function bigpeek()
//The third column toggles in/out. All elements under div right_column.
{
$("div#right-column").toggle("drop", "fast");
}
function smallpeek()
//Smaller snippets like credentials or user assignment flying in/out.
{
$("li.credentials").toggle("drop", "fast");
}
$(document).ready(function() {
$("*").ready(hide_me);
$("*").ready(first_bow);
$(".btn-new-email").click(bigpeek);
$(".button").click(smallpeek);
$(".icon-delete").mouseover(function() {
$(this).effect("bounce", "fast");
});
});
});
The best thing to learn about programming is how to effectively re-use code. In your code, you have set up some functions that you yourself claim will do a bunch of the same thing. So instead, you could make it better by only writing code to do the repeated task once.
For one example, instead of creating a function where you place a bunch of things that need to be hidden, I would add a class to the elements that should be hidden, and then hide all those elements:
function hide_me()
//Hides anything with the "hide-me-onload" class
{
$(".hide-me-onload").hide();
}
$(function () {
...
}
is the same as
$(document).ready(function() {
...
}
So you can move the method calls from inside your $(document).ready() to be inside your $(function(){}). Also try to use IDs instead of class names wherever possible. Something like this will go through the entire DOM to look for an element
$(".item")
Be more specific
$("#itemID") // use IDs instead of Classes
//If you have to use class name then you can speed up the selector by adding the element tag before it
$("div.item")
Using $("*").ready() within $(document).ready() is redundant... you already know using all of the elements are ready! Also, in general using the universal selector $('*') is very inefficient.
So, the first two lines of your $(document).ready() can just be:
hide_me();
first_bow();
Other than that and a couple of issues with organization and nomenclature you're off to a great start, keep it up!

Categories

Resources