Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2011, 11:47 PM   PM User | #1
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
JqueryToggle toggling everything

OK so problem is pretty clear here when i click on any of the section id's all of my p class bottoms open up. Is there a way besides writing class=1,class=2 etc to avoid this issue?

Thanks all!

<section id="clickme"><h3>Latest work</h3>
<img src="images/placeholder.gif" class="placeholder"/>
<p class="bottom">This is the toggle!</p>
</section>

<section id="clickme"><h3>Who we are</h3>
<p class="bottom">This is the toggle!</p>
</section>

<section id="clickme"><h3>Services</h3>
<p class="bottom">This is the toggle!</p>
</section>

<section id="clickme"><h3>Contact Us</h3>
<p class="bottom">This is the toggle!</p>
</section>

<footer>text</footer>
<script type="text/javascript" charset="utf-8">
$("#clickme").click(function () {
$(".bottom").slideToggle("fast");
});
</script>
pcproff is offline   Reply With Quote
Old 04-02-2011, 12:04 AM   PM User | #2
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Elements with the same id is invalid html. Id's must be unique on a page. Classes are used to group elements with the same "name".
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
pcproff (04-02-2011)
Old 04-02-2011, 01:12 AM   PM User | #3
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Thats not what I was asking but thanks for the lesson in W3C standards
pcproff is offline   Reply With Quote
Old 04-02-2011, 01:39 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
no problem

and I think having multiple elements with the same id is part of the problem
bullant is offline   Reply With Quote
Old 04-02-2011, 03:50 AM   PM User | #5
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
bullant is absolutely right — you have to change those ids to classes.

That said, $(".bottom") is a jQuery object containing all elements with the class "bottom", and any method you call on that object will be executed on all the elements it contains.

What you really want to do is calling the method only on the one element that is a child of the clicked element, and that can be achieved with

PHP Code:
$('.clickme').click(function () {
    $(
this).find('.bottom').slideToggle('fast');
}); 
venegal is offline   Reply With Quote
Users who have thanked venegal for this post:
pcproff (04-02-2011)
Old 04-02-2011, 06:14 AM   PM User | #6
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Smile

This is excellent! This is why I love this forum, I came here asking one question and I ended up learning two things! Thanks guys, that "this." is the magic ingredient.
pcproff is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:08 PM.


Advertisement
Log in to turn off these ads.