jquery - Change individual button state -
i have following uses clicks , jquery fade display div when button clicked , change state of button....
html
<a class="link" href="#" data-rel="content1"><img src="http://i.imgur.com/vi1klp9.png"></a> <a class="link" href="#" data-rel="content2"><img src="http://i.imgur.com/u1sbure.png"></a> <a class="link" href="#" data-rel="content3"><img src="http://i.imgur.com/u1sbure.png"></a> <a class="link" href="#" data-rel="content4"><img src="http://i.imgur.com/u1sbure.png"></a> <a class="link" href="#" data-rel="content5"><img src="http://i.imgur.com/u1sbure.png"></a> <div class="content-container"> <div id="content1">this test content part 1</div> <div id="content2">this test content part 2</div> <div id="content3">this test content part 3</div> <div id="content4">this test content part 4</div> <div id="content5">this test content part 5</div> </div>
css
.content-container { position: relative; width: 400px; height: 400px; } .content-container div { display: none; position: absolute; top: 0; left: 0; right: 0; bottom: 0; }
jquery
$(".link").click(function(e) { e.preventdefault(); $('.content-container div').fadeout('slow'); $('#' + $(this).data('rel')).fadein('slow'); $(".link img").attr("src", "http://i.imgur.com/u1sbure.png"); $(this).find("img").attr("src", "http://i.imgur.com/vi1klp9.png"); }); $( document ).ready(function() { $(".link")[0].click(); });
this works great trying have different button / pressed button image each of 5 buttons. can help?
function handler1() { $(this).find("img").attr("src", "http://i.imgur.com/vi1klp9.png"); $(this).one("click", handler2); } function handler2() { $(this).find("img").attr("src", "http://i.imgur.com/u1sbure.png"); $(this).one("click", handler1); } $(".link").one("click", handler1);
another demo --> css approach through class
removed img
tag used a
tags
html
<a class="link active" href="#" data-rel="content1"></a> <a class="link" href="#" data-rel="content2"></a> <a class="link" href="#" data-rel="content3"></a> <a class="link" href="#" data-rel="content4"></a> <a class="link" href="#" data-rel="content5"></a>
css
.link { content:url("http://i.imgur.com/vi1klp9.png"); } .active { content:url("http://i.imgur.com/u1sbure.png"); }
js
$(".link").click(function () { $(this).toggleclass('active'); });
after op's comment
js
$(".link").click(function (e) { e.preventdefault(); $('.content-container div').fadeout('slow'); $('#' + $(this).data('rel')).fadein('slow'); $('.link').removeclass('active'); $(this).toggleclass('active'); }); $(document).ready(function () { $(".link")[0].click(); });
css
.link { content:url("http://i.imgur.com/u1sbure.png"); } .active { content:url("http://i.imgur.com/vi1klp9.png"); }
Comments
Post a Comment