Solved

JQuery Show Hide a specific Class from a list

Posted on 2014-01-22
6
517 Views
Last Modified: 2014-02-01
I have the following piece of JQuery Code That shows a specific div when a specific Radio Button is Clicked.  However, How do I show a specific Div Class, from a list of Div Classes that all have the same name?  The Code is generated by Wordpress, So I can't change the Class Name.  So If I wanted to show the third (3) div "editfield" how would I do that?

<script type="text/javascript">
    $(document).ready(function(){
        $('input[type="radio"]').click(function(){
            if($(this).attr("value")=="Create new team"){
                $(".editfield").toggle();
            }
        });
    });
</script>

<label>
<input id="option_7" type="radio" value="Create new team" name="field_3">
Create new team
</label>

<div class="editfield"> 1 </div>
<div class="editfield"> 2 </div>
[b]<div class="editfield"> 3 </div>[/b]
<div class="editfield"> 4 </div>
<div class="editfield"> 5 </div>
<div class="editfield"> 6 </div>
<div class="editfield"> 7 </div>

Open in new window

0
Comment
Question by:rgranlund
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39800785
How do you determine to the show the third one?

Hardcoded example.
   $(document).ready(function(){
        $('input[type="radio"]').click(function(){
            if($(this).attr("value")=="Create new team"){
                $(".editfield:nth-of-type(3)").show();
            }
        });
    });


http://jsfiddle.net/GaryC123/LDLQ9/
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 39800794
You could use the nth selector:
http://api.jquery.com/nth-child-selector/
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39800836
@ALL
If I changed my script to the following, How do I "unhide a div class when I click on another radio button?

<script type="text/javascript">
    $(document).ready(function(){
        $('input[type="radio"]').click(function(){
            if($(this).attr("value")=="Create new team"){
                  $(".editfield:nth-of-type(3)").hide();
            }
        });
    });
</script>

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 58

Expert Comment

by:Gary
ID: 39800846
<script type="text/javascript">
    $(document).ready(function(){
        $('input[type="radio"]').click(function(){
           $(".editfield").show();
            if($(this).attr("value")=="Create new team"){
                  $(".editfield:nth-of-type(3)").hide();
            }
        });
    });
</script>

Open in new window



Not so sure if you are wanting to hide then show or show then hide - so maybe you need to swap the hide/show bits
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39801679
You can also use the eq() method:

$(".editfield").eq(2);

It's zero based, so the first element is 0, the second is 1, the third is 2 etc.

If you want to pass in the value dynamically, there's a couple of ways of doing it. Here's an example using the data attribute:

<input type="radio" value="Value 1" name="myRadio" data-divtoshow='0' />
<input type="radio" value="Value 2" name="myRadio" data-divtoshow='1' />
<input type="radio" value="Value 3" name="myRadio" data-divtoshow='2' />
<input type="radio" value="Value 4" name="myRadio" data-divtoshow='3' />

$('input[type="radio"]').click(function(){
    $(".editfield").hide().eq($(this).data('divtoshow')).show();
});

Open in new window

http://jsfiddle.net/ChrisStanyon/6FZRA/
0
 
LVL 58

Expert Comment

by:Gary
ID: 39826905
Can you explain why you have accepted this answer? When it certainly would not work with your code.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now