Solved

JQuery Show Hide a specific Class from a list

Posted on 2014-01-22
6
515 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 42

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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

17 Experts available now in Live!

Get 1:1 Help Now