Reputation: 312
i'm using some jquery code to swap an image on click to a new source, but i need it to add the class of "active" on click and remove it when another elemnt is clicked, heres the jquery and html
html
<a class="steplink" href="http://placebox.es/600/500/ff0000/ffffff/Step1/">Step1</a>
<a class="steplink" href="http://placebox.es/600/500/ff0000/ffffff/Step2/">Step2</a>
<a class="steplink" href="http://placebox.es/600/500/ff0000/ffffff/Step3/">Step3</a>
<div id="stepimgwrap">
<img src="http://placebox.es/600/500/ff0000/ffffff/Step1/" alt="Main Image" id="stepimg"/>
</div>
jQuery
$('.steplink').live("click", function() {
$('#stepimg').hide();
$('#stepimgwrap').css('background-image', "url('../img/ajax-loader.gif')");
var i = $('<img />').attr('src',this.href).load(function() {
$('#stepimg').attr('src', i.attr('src'));
$('#stepimgwrap').css('background-image', 'none');
$('#stepimg').show();
});
return false;
});
Thanks in advance
Upvotes: 0
Views: 1714
Reputation: 21
here including the CLICK event
$(document).ready(function(){
$('.steplink').click(function(){
$('#stepimgwrap').toggleClass('active');
return false;
//
});
});
Upvotes: 1
Reputation: 3445
$('.steplink').click(function(e) {
e.preventDefault();
$('.steplink').removeClass('active');
$(this).addClass('active');
var href = $(this).attr('href');
$('#stepimg').attr('src', href);
});
This adds active class to current anchor and changes the src of the image.
Upvotes: 0
Reputation: 74738
Try this one:
$('.steplink').live("click", function() {
$('#stepimg').hide();
$(this).addClass('active'); // <---------------add active class to link clicked
$(this).siblings().removeClass('active'); // <-----remove the other link active class
$('#stepimgwrap').css('background-image', "url('../img/ajax-loader.gif')");
var i = $('<img />').attr('src',this.href).load(function() {
$('#stepimg').attr('src', i.attr('src'));
$('#stepimgwrap').css('background-image', 'none');
$('#stepimg').show();
});
return false;
});
Upvotes: 2
Reputation: 77986
Normally I'd use siblings()
but your DOM doesn't isolate all the steplink
s
$('.steplink').live("click", function() {
/* .. blah blah */
$('.steplink').removeClass('active');
$(this).addClass('active');
});
Upvotes: 0
Reputation: 14434
you want to do it on the stepimg?
$('#stepimg').toggleClass('active');
Upvotes: -1