PDA

View Full Version : simple jquery help!




Me1000
Sep 29, 2007, 08:11 PM
Is anyone familiar with jQuery?

I have a div called testidrop,
<div class="testidrop" id="testidrop">...</div>

normally is is hidden, but when I click on a link (testialink)
it should drop down,

<a href="#" id="testialink" class="testialink">Testimonials</a>

So I thought it was all working great, until I discovered than whenever you click ANYWHERE on the page it drops down!
:confused:

Here is the code in the head tag

<script>
//show testimonials
$(document.getElementById('testialink')).click(function(){$(document.getElementById('testidrop')).sl ideToggle("slow")});

$(document.getElementById('close')).click(function(){$(document.getElementById('testidrop')).slideup ("slow")});
</script>
(BTW the reason for the close one, us because there is a link within the testidrop div that will close it)


any idea why this happens? or what I can do to fix it?


Big Thanks,



Matteh117
Oct 1, 2007, 05:07 AM
Not tested it, but try this;
<script>
$(document).ready(function(){
$(document.getElementById('testialink')).click(function(){
$(document.getElementById('testidrop')).slideToggle('slow');
});
$(document.getElementById('close')).click(function(){
$(document.getElementById('testidrop')).slideup('slow');
});
}
</script>

bootedbear
Oct 1, 2007, 10:31 AM
Why are you calling getElementById? jQuery will do all that for you!

$('#elementId').click( ... )

bootedbear
Oct 1, 2007, 10:33 AM
Converting your first example:

$('#testialink').click(function(){$('#testidrop').slideToggle('slow');});

bootedbear
Oct 1, 2007, 10:38 AM
$('#close').click(function(){$('#testidrop').slideUp('slow')});

Me1000
Oct 1, 2007, 04:39 PM
I tried to do the #=id thing, but it never worked,

this is what I finally got to work

$(document).ready(setTestimonials);

function setTestimonials() {

$('#testialink').click( function(){ $('#testidrop').slideToggle('slow') } );
$('#close').click( function(){ $('#testidrop').slideUp('slow') } );

}

Matteh117
Oct 2, 2007, 04:37 AM
Try adding "name" alongside the "id" and changing the jQuery as suggested. ;)