0

I'm trying to make a function that will target multiple divs, one at a time depending on a variable. So if they click the button once it will show the tip with ID of #tip1, if they click it again, it will show #tip2 etc.

        <script>
        var tipCount = 0;
        $('.subtitle').click({
            function () ({
                $('#tip'+tipCount++).toggleClass('visible');
            });
        });         
        </script>

I've been google for well over an hour and just can't get it it to work!

2
  • post your complete code. the given should have worked Commented Jan 30, 2013 at 7:08
  • How could the given work? That's not proper syntax. Commented Jan 30, 2013 at 7:09

4 Answers 4

1
    var tipCount = 0;
    $('.subtitle').click(
        function () {
            $('#tip'+tipCount).hide();
            $('#tip'+(tipCount++)).show();
        });     
Sign up to request clarification or add additional context in comments.

Comments

0
var tipCount = 0;
$('.subtitle').click(function(){
    $('#tip'+tipCount++).toggleClass('visible');
});

Given your syntax, probably got an error on (

Comments

0

The main issue is the incorrect format of the javascript. Here is a working proof of method.

html

<h2 class="subtitle">Subtitle</h2>

<div id="tip1">This is tip 1</div>
<div id="tip2">This is tip 2</div>
<div id="tip3">This is tip 3</div>
<div id="tip4">This is tip 4</div>
<div id="tip5">This is tip 5</div>

css

.hidden{
    display: none;
}

javascript

var tipCount = 0;
$('.subtitle').click(function(){
    $('#tip'+tipCount++).toggleClass('hidden');
});

Demo: http://jsfiddle.net/7sgRu/

Comments

0

HTML

<button>bum</button
<div class="hidden" id="tip1">This is tip 1</div>
<div class="hidden" id="tip2">This is tip 2</div>
<div class="hidden" id="tip3">This is tip 3</div>
<div class="hidden" id="tip4">This is tip 4</div>
<div class="hidden" id="tip5">This is tip 5</div>

CSS

.hidden{
    display: none;
}

JavaScript

var tipCount = 1;
$('button').click(function(){
    if(tipCount==6){
        $('#tip5').addClass('hidden');
        tipCount=1;
    }    
    $('#tip'+tipCount).removeClass('hidden');
    tipCount--;
    $('#tip'+tipCount++).addClass('hidden');
    tipCount++;
});      

Demo: http://jsfiddle.net/7sgRu/4/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.