Reputation: 33
I would like to add "-h.png"
to the end of all img src in < div id="swap" >
at the same time when mouseover/hover over < img src="images/logo.png" />
and return back to ".png"
mouseout
This is what I have and it's not working:
<div id="header-wrap">
<div id="swap">
<img src="images/4.png"/>
<img src="images/3.png"/>
<img src="images/2.png"/>
<img src="images/1.png"/>
</div>
<header id="header">
<div id="site-logo"><a href="#"><img src="images/logo.png" /></a></div>
</header>
</div><!-- /#header-wrap -->
$(document).ready(function() {
$('#site-logo').hover(function(){
$('#swap img').replace('.png','-h.png');
},
function(){
$('#swap img').replace('-h.png','.png');
});
});
Just updated to below... the images are swapping now but all 4 images swapped to /4-h.png instead of 4-h.png, 3-h.png, 2-h.png and 1-h.png
$(document).ready(function() {
var newSrc = "";
$('#site-logo').hover(function(){
newSrc = $('#swap img').attr('src').replace('.png','-h.png');
$('#swap img').attr('src', newSrc);
},
function(){
newSrc = $('#swap img').attr('src').replace('-h.png','.png');
$('#swap img').attr('src', newSrc);
});
});
Upvotes: 3
Views: 4080
Reputation: 1409
try this :
/* so it's not working
$(document).ready(function() {
$('#site-logo').hover(function(){
$('#swap img').attr('src').replace('.png','-h.png');
},
function(){
$('#swap img').attr('src').replace('-h.png','.png');
});
});
*/
ok so I figured out the .replace method is pure a javascript try this :
$(document).ready(function() {
var newSrc = "";
$('#site-logo').hover(function(){
$('#swap img').each(function() {
newSrc = $(this).attr('src').replace('.png','-h.png');
$(this).attr('src', newSrc);
});
},
function(){
$('#swap img').each(function() {
newSrc = $(this).attr('src').replace('-h.png','.png');
$(this).attr('src', newSrc);
});
});
});
Upvotes: 1
Reputation: 6996
$("#site-logo").hover(function () {
$("#swap img").each(function () {
var test = $(this).attr('src');
$("#helper").append("<span>" + test.replace('.png', '-h.png') + "</span><br />");
});
},
function () {
$("#swap img").each(function () {
var test = $(this).attr('src');
$("#helper2").append("<span>" + test.replace('-h.png', '.png') + "</span><br />");
});
});
Upvotes: 0
Reputation: 74738
You can try this one just use .slice()
this way and you can achieve your goal:
$('#site-logo').hover(function () {
var atr = $('#swap img').attr('src').slice(0, -4);
var newAtr = atr+'-h.png'
$('#swap img').attr('src', newAtr);
},function () {
var atr = $('#swap img').attr('src').slice(0, -6);
var newAtr = atr+'.png'
$('#swap img').attr('src', newAtr);
});
checkout the fiddle: http://jsfiddle.net/6vqJV/
Upvotes: 1
Reputation: 2190
Try this code
$(document).ready(function() {
$('#site-logo').hover(function(){
$('#swap img').attr('src','-h.png');
},
function(){
$('#swap img').attr('src','.png');
});
});
OR
$(document).ready(function() {
$('#site-logo').hover(function(){
$('#swap img').each(function(){
$(this).attr('src','-h.png');
});
},
function(){
$('#swap img').each(function(){
$(this).attr('src','.png');
});
});
});
Upvotes: 0