0

I have this segment of code

$('#test').click(function(){
 $(this).hide();
 $('#hidethis').show();
});
$('#test2').click(function(){
  $('#hidethis').hide();
  $('#test').show();
})

This Code works fine, I'm however wanting to apply it to more elements. So i want to ideally add more ID's

I have tried this method which hasn't worked.

$('#test,#test3').each.click(function(){
 $(this).hide();
 $('#hidethis').show();
});
$('#test2,#test4').each.click(function(){
  $('#hidethis').hide();
  $('#test').show();
})

Will add a Fiddle if needed

4
  • 3
    use class instead of id Commented Oct 1, 2015 at 9:08
  • 2
    you don't need each --- $('#test,#test3').click(function(){}); should do Commented Oct 1, 2015 at 9:08
  • 1
    .each is a function that you supply a callback to, it doesn't have a .click. You should be getting an error on the console. Commented Oct 1, 2015 at 9:13
  • Make a fiddle please, it is needed Commented Oct 1, 2015 at 9:32

2 Answers 2

1

All you need to do is the following:

$('#test, #test3').click(function(){
 $(this).hide();
 $('#hidethis').show();
});

$('#test2, #test4').click(function(){
  $('#hidethis').hide();
  $('#test').show();
});

JSFIDDLE EXAMPLE

Sign up to request clarification or add additional context in comments.

1 Comment

Wasn't working for me last night, Is now! How weird, must have had something wrong
0

$('.test').click(function () {
    $('.test').each(function(){
        $(this).hide();
    });
    $('#hidethis').show();
});

$('#hidethis').click(function () {
    $(this).hide();
    $('.test').show();
})
.test {
    display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hidethis">HIDE THIS</div>
<div class="test">SHOW ME</div>
<div class="test">SHOW ME1</div>
<div class="test">SHOW ME2</div>
<div class="test">SHOW ME3</div>

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.