![]() |
Help with jQuery
I've just started looking at JQuery today and I have made the following 'simple' script to play with:
html4strict Code:
Basically there are three elements. 1 & 3 are little links. I want element 2 to be hidden when element 1 is clicked, and then reshown when element 1 is clicked again. This is the first time I have looked at JQuery (or any kind of JScript) so any help would be appreciated. With the script at the minute, it hides element 2 but also element 3 which I don't want it to do. |
The following code is using toggle effect and is not tested, but I believe it should work:
HTML Code:
<script type="text/javascript">HTML Code:
$('#fixtures').toggle('slow');Yours, Runar |
That's great! But when I click element 1, it still toggles elements 2 & 3, whereas I just want it to hide element 2 and leave element 3 in place.
Thanks, |
Your HTML must be broken and/or not what was posted above. Element 3 must be contained within Element 2 for it to be hidden when the link is clicked.
|
Speaking of your HTML. I suggest you read this article about a phenomena called Div Mania.
In short, you should not overuse div elements but instead use CSS selectors (classes and ids). Take a look at my example: Your way: HTML Code:
<div id="hidefixtures">HTML Code:
<a href="" id="hidefixtures">Hide/Show</a>Yours, Runar |
Getting there
Ok, bearing in mind this JQuery is all new to me today how would I go about doing this?
I have 2 or more elements that have the same features (click to hide, click to show). Each element also has within it a click to show even more info, and then click to hide more info (using toggle function. The thing is, the code is starting to get repetative - there must be a way to eliminate some of the code? Here's what I have.... for my javascript in the head section: javascript Code:
and for each HTML element: HTML Code:
<div class="wrapper">HTML Code:
<div class="wrapper"> |
Your elements may have the same features, but they are still not exactly the same. I am not sure if you can use the same function to toggle a div container and a child element of that div.
jQuery is not my best field either, so I will have to pass it on to someone else. Instead, I will give you a few more tips when it comes to HTML: If you want to display a list, use the list element. It comes in two variations: ul (unordered lists) and ol (ordered lists). The main list element (ul or ol) works as a container, with several child elements (li) inside. Your code would for example look like this: HTML Code:
<ul id="resinfo" class="p">Also, if you wish to apply more than one class to an element, simply enter the classes inside the same attribute: HTML Code:
<h1 class="first-class second-class">Lorem ipsum</h1>Yours, Runar |
Thanks for the tips on HTML but I got that under my belt (I think ;-)). That script was just a quick mock-up to play around with JQuery.
Thanks for your help though :-) - anybody else? |
You could stop the repeated JavaScript by making use of appropriate classes (on which to hook events, animations, etc.) rather than referencing individual elements by ID.
For example: JavaScript Code:
Assuming your HTML blocks are like (note they're changed a bit from yours above): HTML Code:
<div class="wrapper">…parent().next…) are traversing the document to get from the link to the element to be shown/hidden. |
| All times are GMT. The time now is 02:40 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0