I have three buttons that change color when you hover (sprites) and I want to use those buttons so that when they are clicked on, different content will display.
I’ve gone through multiple tutorials / boards and nothing seems to work.
The buttons are displayed as follows:
<div id="buttons">
<a href="#" id="1"class="des"></a>
<a href="#" id="2"class="brnd"></a>
<a href="#" id="3"class="strt"></a>
</div>
The divs where the content was placed (my last attempt) were as follows:
<div id="pages">
<div id="div1"><img src="..."></></div>
<div id="div2"><img src="..."></></div>
<div id="div3"><img src="..."></></div>
jQuery—-
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
$("a").click(function() {
var id = $(this).attribute("data-id"); // Using a custom attribute.
$("#pages div").hide(); // gather all the div tags under the element with the id pages and hide them.
$(".div" + id).show(); // Show the div with the class of .divX where X is the number stored in the data-id of the object that was clicked.
});
});
</script>ââââ
My anchors seem to be wrong. With this setup it just went to the top of the page. When using the anchor #1, #2, or #3 it would go to the div location but it would not hide or show the content. Frustrating.
The sprites are working fine. Now I’m trying to figure out how to make them clickable so that different content will display when each button is clicked (3 divs – under a parent div?). If anyone knows exactly how to do this I will be so thankful.
The content is largely images, and I am using Jupiter theme with a front-end editor so I don’t know if it could be something with that. But nothing seems to be broken on the backend.
Also, if you can point me to a tutorial that will teach me how to make it so they animate in and out when clicked, that would be legit. Thanks again.
Check out my fiddle.
The code you posted had two main problems, confusing jQuery’s data attributes with Javascript id’s, and confusing the CSS selectors for classes (
.
) and ids (#
). Here is the corrected html and javascript:HTML
Javascript
you can Simple Use hide and show
Here Your Id for the button is Buttons
You can Use .Toggle() to switch between states
so
You can Also Use
$("#Buttons").onclick(function(){
Instead ofclick
depends on Your jquery versionSee the Link May be you want this
Try this:
check tutorial here http://api.jquery.com/show/
http://jsfiddle.net/9SabV/
i have updated my answer as you updated your html,script code.
check this also
Use # for id and . for class.
http://jsfiddle.net/mdgd7/
There is only one issue I found in your code ie
here instead of “.”(dot), you have to use “#”.
You have to replace it with this:-
because you are using “id” in “div”, not “class”.
UPDATE
You have have to remove this:
instead you have add this to css file:-
Here is the updated js script:-
There are certain issues in you code. First of all you have not added data-id attribute to the anchor tags and tried referring them in you js code. The html5 “data-” attribute is used to store custom data inside the tag.The logic behind this is that any attribute with a “data- prefix” will not be processed and will be rendered as a data element.
Then the closures that you have added after the image tag are not necessary and syntactically wrong and is an error.
Here in your case, we can simply handle it with the “id” as there is not much need to use the data-attribute.
The simplified code will be like,
HTML
JS
CSS
Animations : I have added fadein animation. For simple fade in, fade out effects you can use jquery short hand animations like .slideDown() .fadeIn(), .animate(). There are more options available here : http://api.jquery.com/animate/
However you can add more customized animations using CSS3 animations which are more browser friendly. The criteria to choose is that, if you need more control over the animations and event tracking you can go for jQuery animations and otherwise CSS3 animations works like a charm.