Trying to change image source with Javascript, not working - javascript

I'm including the Javascript (jQuery really) in order to change the img src in question from one image to another image on hover.
However this isn't working - the images don't change at all.
My only guess is that the unnamed functions aren't doing what I want them to do.
My knowledge of jQuery is still somewhat new, so I'm stumped here and can't find any resources on what I might be doing wrong.
jQuery("#fbicon").hover(
function () {
jQuery(this).attr("src","images/nohoverfb.png");
},
function () {
jQuery(this).attr("src","images/fb.png");
});
jQuery("#linkedinicon").hover(
function () {
jQuery(this).attr("src","images/nohoverlinkedin.png");
},
function () {
jQuery(this).attr("src","images/linkedin.png");
});
jQuery("#googleplusicon").hover(
function () {
jQuery(this).attr("src","images/nohovergoogle+.png");
},
function () {
jQuery(this).attr("src","images/google+.png");
});
jQuery("#youtubeicon").hover(
function () {
jQuery(this).attr("src","images/nohoveryoutube.png");
},
function () {
jQuery(this).attr("src","images/youtube.png");
});
jQuery("#twittericon").hover(
function () {
jQuery(this).attr("src","images/nohovertwitter.png");
},
function () {
jQuery(this).attr("src","images/twitter.png");
});
jQuery("#emailicon").hover(
function () {
jQuery(this).attr("src","images/nohoveremail.png");
},
function () {
jQuery(this).attr("src","images/email.png");
});
Here is an example of the HTML:
<span class="icon"><a target="_blank" href="http://www.facebook.com/facebookname"><img id="fbicon" src="http://www.website.com/sites/all/themes/website/images/fb.png" style="height:48px;width:48px"></a></span>

I think you are making a mistake in order of functions:
Try this:
$("#fbicon").hover(
function () {
jQuery(this).attr("src","images/fb.png"); //hover in function
},
function () {
jQuery(this).attr("src","images/nohoverfb.png"); //hover out function
});
Change all images accordingly in your code, and that should do what you want it to.
See the demo fiddle which changes background css to red on hover and blue when mouse leaves that div.
Hope it helps.
EDIT
I made a fiddle, with your html, which uses 2 images from internet. See that images are changed in Hover in, Hover out.
HTML:
<span class="icon"><a target="_blank" href="http://www.facebook.com/facebookname"><img id="fbicon" src="http://s2.reutersmedia.net/resources/r/?m=02&d=20141020&t=2&i=985015946&w=580&fh=&fw=&ll=&pl=&r=LYNXNPEA9J0XH" style="height:48px;width:48px"></a></span>
jQuery:
$("#fbicon").hover(
function () {
jQuery(this).attr("src","http://www.gizbot.com/img/2014/10/14-1413281227-sonyxperiaz3cropedimages600x450withwatermark3.jpg"); //hover in function
},
function () {
jQuery(this).attr("src","http://s2.reutersmedia.net/resources/r/?m=02&d=20141020&t=2&i=985015946&w=580&fh=&fw=&ll=&pl=&r=LYNXNPEA9J0XH"); //hover out function
});

Related

slideUp and slideDown jQuery

I have a table in which I can click the rows () with the class .details. This will show a div with id="details" with extra information about the element in the row.
I have the following code.
$('.details').click(function () {
$('#details').slideUp('slow');
$('#details').load($(this).data('url'), { id: $(this).data('id') }, function () {
$(this).slideDown('slow');
});
});
However I would like the loading (.load()) to happen after the .slideUp() due to the fact that the load starts while the element is sliding up (which looks wierd). I have tried to add it as a callback function the following way:
$('#details').slideUp('slow', function () {
$('#details').load($(this).data('url'), { id: $(this).data('id') }, function () {
$(this).slideDown('slow');
});
});
However that stops the code from working. Does anyone have an idea on how to solve this?
Thanks.
EDIT:
My table row looks as follows:
<tr class="details" data-id="#item.VehicleID" data-url="#Url.Action("Details", "Vehicle")">
</tr>
My div looks as follows:
<div id="details"></div>
One problem i see with your code is, you are using $(this).data('url') to get the url data attribute value set to the tr which was clicked. but $(this) is actually the $('#details') there because you are accessing it inside the slideUp of $('#details'), which does not have the url data attribute. So you must be getting an error
The solution is to assign the $(this) (clicked row) to a local variable and use that inside your other callback function.
This should work.
$(function () {
$('.details').click(function () {
var _this = $(this);
$('#details').slideUp('slow', function () {
$('#details').load(_this.data('url'), { id: _this.data('id') }, function () {
$('#details').slideDown('slow');
});
});
});
});

Execute JqueryMobile Popup on Jquery timing event

I am having trouble attaching a timing event to my function I want this to on execute the function after 25 seconds. what am I doing wrong?
setTimeout("ajaxTimeout();", 25000);
$(document).on({
//open popup here
'pageshow': function ajaxTimeout(){
$('#askforsomething').popup('open');
}
}, '#homepage');
Two points:
You probably mean $(document).ready(function () { ... }). Alternatively, a shorthand for this is simply $(function () { ... }).
You can (and should) pass a function to setTimeout instead of a code string.
Result:
$(function () {
setTimeout(function () {
$('#askforsomething').popup('open');
}, 25000);
});
I don't know all the logic behind it but this did work for me. and the guy above looks like he was pretty close to the same.
$(document).on({
//open popup here
"pageshow": function () {
setTimeout("$('#askaquestion').popup('open');", 15000);
}
}, "#homepage");

jQuery image swap and animate not working

I'm trying to do a simple image swap, but when I add in my code for the swap, it doesn't work! I have a function toggling different classes which are animated using CSS which work fine without the image swap code, but once I add it in all of it breaks!
Could someone troubleshoot my code really quickly? I feel like my JQuery logic is a bit off.
jQuery(document).ready(function () {
var toggle = 0;
var toggleClass = function () {
toggle = !toggle;
$(".two").toggleClass("two-menu", toggle);
$(".four").toggleClass("four-menu", toggle);
$(".images").toggleClass("images-menu", toggle);
$(".home").toggleClass("home-menu", toggle);
$("#bottom-left").toggleClass("bottom-left", !toggle);
$("#bottom-right").toggleClass("bottom-right", !toggle);
$("#margin-zero").toggleClass("margin-zero", !toggle);
$(".left-container").toggleClass("left-container-show", toggle);
$(".right-container").toggleClass("right-container-show", toggle);
}
var imageSwap = function () {
this.src = '/wp-content/uploads/2013/11/Twitter.jpg';
}, function () {
this.src = '/wp-content/uploads/2013/11/Facebook.jpg';
}
jQuery(".home").click(function () {
toggleClass();
});
jQuery(".two").click(function () {
toggleClass();
imageSwap();
});
jQuery(".four").click(function () {
toggleClass();
});
});
I've created two JSFiddles.
1) The first does not work, and includes the imageSwap function. http://jsfiddle.net/MuQ2w/
2) The second does not have the imageSwap, and works perfectly. http://jsfiddle.net/E2Rzv/
The problem you are experiencing is because of the syntax of you jQuery imageSwap function, as you can't write two function seperated with a "comma".
I think a possible solution might be to remove the second function.
Also imageSwap function doesnot know about 'this' as it is out of it's scope. You'll need to pass 'this' as an argument to it.
So the final imageSwap function will look like:
var imageSwap = function ($this) {
$this.src = '/wp-content/uploads/2013/11/Facebook.jpg';
}
And your call to this function would be like:
jQuery(".two").click(function () {
imageSwap(this);
toggleClass();
});
I hope it will help.
P.S. Just to follow the tradition, here is a working fiddle:
jsfiddle.net/gKxLz
did you look in your console for the errors? `
var imageSwap = function () {
this.src = '/wp-content/uploads/2013/11/Twitter.jpg';
}, function () {
this.src = '/wp-content/uploads/2013/11/Facebook.jpg';
}
is not correct syntax: the second function has no variable name
It is hard to figure out what is happening without debugging the code.
I am guessing that perhaps the problem is due to the scope of "this" inside imageSwap function.
Try to pass this (for the handler) to imageSwap function as a parameter, like:
var imageSwap = function ($this) {
$this.src = '/wp-content/uploads/2013/11/Twitter.jpg';
}, function ($this) {
$this.src = '/wp-content/uploads/2013/11/Facebook.jpg';
}
jQuery(".two").click(function () {
toggleClass();
imageSwap(this, this);
});
Hope it will work

Prevent other functions running after selected function

I have a div #imageSwap and three buttons #button1, #button2, #button3. The first two buttons change the HTML of the div to hold a specific image, the third starts a rotation of multiple images.
For some reason, when the third (rotating) button is clicked it runs fine, but when the first or second button is clicked after the third, the animation runs when it shouldn't.
<script language="javascript" type="text/javascript">
$(document).ready(function () {
setImageOne();
$('#button1').addClass('selected');
$("a#button1").click(function() {
setImageOne();
$(this).addClass('selected');
$('#button2').removeClass('selected');
$('#button3').removeClass('selected');
});
$("a#button2").click(function() {
commObj();
$(this).addClass('selected');
$('#button1').removeClass('selected');
$('#button3').removeClass('selected');
});
$("a#button3").click(function() {
comProg();
$(this).addClass('selected');
$('#button1').removeClass('selected');
$('#button2').removeClass('selected');
});
});
function setImageOne() {
$('#imageSwap').fadeIn(500).html('<img src="slide1.png" />');
}
function commObj() {
$('#imageSwap').fadeIn(500).html('<img src="co.png" />');
}
function comProg() {
$('#imageSwap').fadeIn(500).html('<img src="bo2.png" />').delay(2000).fadeOut(500, function () { comProg2(); });
}
function comProg2() {
$('#imageSwap').fadeIn(500).html('<img src="bo2.png" />').delay(2000).fadeOut(500, function () { comProg3(); });
}
function comProg3() {
$('#imageSwap').fadeIn(500).html('<img src="bo2.png" />').delay(2000).fadeOut(500, function () { comProg4(); });
}
function comProg4() {
$('#imageSwap').fadeIn(500).html('<img src="bo2.png" />').delay(2000).fadeOut(500, function () { comProg(); });
}
</script>
That is because all your comProg functions have a callback function that calls a comProg function and because of that the animation will run every 2 seconds.
And btw you can write comProg2 instead of function () { comProg2(); } although all your comProg functions are same you could just use one function and use a callback that would call the same function instead of the next function which does the same thing...
check out jquery animation queue http://api.jquery.com/queue/
Add something like
$('#imageSwap').clearQueue();
in the callback handlers for button 1 and button 2 to stop the endless animation.
See http://api.jquery.com/clearQueue/

How to hide div on mouseout?

I'm trying to achieve a simple hover effect. When a user hovers an image (a view filled with images), it should show an extra div. No problem there.
But when I want to do the opposite thing, hide the same div when the user leaves the area of the div, it doesn't work: the div won't go away when leaving the div area (which should be done by default when using the hover, no?).
This is the code I use:
$(document).ready(function () {
$('.views-field-field-photo-fid').out(function () {
showDiv($(this), $('.views-field-title'));
});
$('.views-field-field-photo-fid').out(function () {
hideDiv($(this), $('.views-field-title'));
});
});
function showDiv(sender, object) {
$(object).show();
}
function hideDiv(sender, object) {
$(object).hide();
}
So far, I've tried .mouseenter, .mouseleave, .hover, .out, but nothing works to hide the div after leaving the div area.
UPDATE
I found the solution, more or less:
$(document).ready(function() {
$('#uicarousel-news-preview-block-2 .views-field-field-photo-fid').hover(
function() { $(this).find('.views-field-title').show(); },
function() { $(this).find('.views-field-title').hide(); }
)
});
But since there are a lot of .views-field-title, it's hiding/showing them too. So I figured to use the find, but no solution there.
Any ideas?
There is no jQuery function called out, I am not sure whether you are using a plugin or not. Anyway, this how a correct hover declaration should look like:
<script style="text/javascript">
$(document).ready(function() {
$('.views-field-field-photo-fid').hover(
function() {
showDiv($(this), $('.views-field-title'));
},
function() {
hideDiv($(this), $('.views-field-title'));
});
});
function showDiv(sender, object) {
$(object).show();
}
function hideDiv(sender, object) {
$(object).hide();
}
</script>
I suggest that you take a look at the jQuery.hover documentation.

Categories

Resources