tags:

views:

76

answers:

5

I want to add a + button to div 1 only if JavaScript enabled (otherwise it should not be there) and when user will click on + button then div 2 will be open/close like accordion.

if div 2 is opened then + icon should be changed into -

by default div 2 should be closed and if JavaScript would be disabled then it should open by default and +/- icon should be hidden.

I want to do this with jquery.

alt text

Edit:

This code is working good for me ,

http://stackoverflow.com/questions/3044331/how-to-make-jquery-slidetoggle-effect-for-multiple-div-on-same-page/3044662#3044662

I just want to add +/- icon also using JavaScript, if JavaScript would be disabled then it should open by default and +/- icon should be hidden.

I will use image of + and - not text.

A: 

Use jQuery to create an accordion control for you and only have divs as the base html.

So basically you have the accoridon plug in loaded and if javascript is enabled then the accordion will work else you will see a set of divs so that users can still see the data in the panesl.

is this what you're asking?

griegs
+4  A: 

This answer shows div2 and hides the plus when no JS.

A background image on the plus span is used instead of an img tag as semantically it isn't an image but a button which is represented in image form.

CSS:

.plusClass 
{ 
    display: none; 
    width:IMGWIDTH; 
    height: IMGHEIGHT; 
    float:right; 
    display: block;
    margin-right: 15px;
}
    .contracted .plusClass { background-image: url('plus.png'); }
    .expanded .plusClass { background-image: url('minus.png'); }

.div2class { display: block; }

HTML:

<div class="div1">Title<span class="plusClass"></span></div>
<div class="div2">Content</div2>

JS:

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('.plusClass').show();
    $('.div2class').hide();
    $('.plusClass').addClass('plusContracted');
    $('.plusClass').click(function() {
        $(this).parent().next().toggle;
        /* Thanks to Chadwick for toggleClass */
        $(this).parent().toggleClass('expanded').toggleClass('contracted'); 
    });
});
</script>
Graphain
you'll want to fix the toggleClass(..) call. My comment to my post was untested, and not correct use.
Chadwick
Thanks so I do. I looked it up and everything but misread it. As I had it the div would have either "expanded contracted" or "" not "expanded" or "contracted".
Graphain
A: 

try this:

$(function(){
     $("#div2").hide();
     $("#div1 #divIcon").text("+");

     $("#div1").click(function(){
        var t=$("#div1 #divIcon").text();
        if(t=='+'){
            $("#div1 #divIcon").text("-");
            $("#div2").show();
        }else{
            $("#div1 #divIcon").text("+");
            $("#div2").hide();
        }
     });
});

PS:- not tested but should work though there are many elegant ways and plugins to do this.

TheVillageIdiot
+1  A: 

This hides div2 and adds the toggle button in the ready function, which obviously only happens if js is present.

The click is bound to a function to slide toggle div2 and change the button.

Style to taste.

<script type="text/javascript">
    $(document).ready(function(){
        $('#div2').hide();
        $('#div1').append('<span id="btn">+</span>');
        $('#btn').click(function(){
            $('#div2').slideToggle(600);
            $(this).text($(this).text() == '+' ? '-' : '+');
        });
    });

</script>

<div id='div1'><h2>div 1</h2></div>
<div id='div2'><h2>div 2</h2>Lorem ipsum dolor sit amet</div>

UPDATE: If the button should be an image, I'd recommend toggling the class of a div with a background image. Same ideas though.

<style>
#btn {float: right;width:16px; height:16px; }
.plus { background-image:url('plus-icon.gif');}
.minus { background-image:url('minus_icon.gif');}
</style>


$('#div2').hide();
$('#div1').prepend('<div id="btn" class="plus"></div>');
$('#btn').click(function(){
    $(this).toggleClass('plus').toggleClass('minus');
    $('#div2').slideToggle(600);
});
Chadwick
Thnaks. but i will use images for + and - not text.
metal-gear-solid
This answer is neater but mine covers images. It's not that hard to combine the two.
Graphain
In that case I'd suggest using a background image on the span via css classes and using `toggleClass('plus','minus')` on the button span (of course giving it an initial class). Same concept though.
Chadwick
toggleClass function nice
Graphain
This answer is pretty neat and covers it all now
Graphain
@Chadwick - you solution is nice to add image but the problem is it's adding both plus and minus.
metal-gear-solid
@metal-gear-solid - There is no problem, my comment above is wrong, but the updated code is correct: it is _toggling_ both plus and minus. The div element begins with plus class, and the toggle action removes 'plus' and adds 'minus'... or vice-versa.
Chadwick
+1  A: 

demo

fixed demo based on comments

css

.toggle img {
    display:none;
}​

html

<div class="toggle">Show Panel <img src="http://t3.gstatic.com/images?q=tbn:4TZreCjs_a1eDM:http://www.venice.coe.int/images/plus.png"&gt;&lt;/div&gt;
<div class="hidden">
Blah Blah Blah
</div>​

jQuery

$(".hidden").hide();
$('.toggle img').show();
$(".toggle").click(function(){
    $(".hidden").slideToggle("slow");
        $(this).html(function(i,html) {
            if (html.indexOf('Show') != -1 ){
               html = html.replace('Show','Hide');
            } else {
               html = html.replace('Hide','Show');
            }
            return html;
        }).find('img').attr('src',function(i,src){
        return (src.indexOf('plus.png') != -1)? 'http://t1.gstatic.com/images?q=tbn:1PS9x2Ho4LHpaM:http://www.unesco.org/ulis/imag/minus.png' :'http://t3.gstatic.com/images?q=tbn:4TZreCjs_a1eDM:http://www.venice.coe.int/images/plus.png';
    });
});
Reigel
what will happen when JS is disabled?
metal-gear-solid
I think with that codes you can do it already by yourself... do some css to the image, hide it... then show image using jQuery :)
Reigel
@Reigl - in your example Div2 not showing if js is disabled.
metal-gear-solid
yes, okay here's a fixed demo http://jsfiddle.net/XPXzs/
Reigel
Whatever you tell your CSS to do.
Graphain