0

Hi Im having troubles with getting the following to work. Only the first element works, how should I think?

<div class="hi">
    <a id="tabBtn" href="javascript:void(0)"></a>
</div>
<div class="hi">
    <a id="tabBtn" href="javascript:void(0)"></a>
</div>
<div class="hi">
    <a id="tabBtn" href="javascript:void(0)"></a>
</div>

<script>
    $('#tabBtn').on("click",function(){
        if ($(this).parent('.hi').css('max-height') == '65px'){
            $(this).parent(".hi").addClass('open');
        }
        else{
            $(this).parent(".hi").removeClass('open');
        }
    })
</script>
2
  • 1
    you can't use same id's for multiple elements Commented Apr 6, 2016 at 14:18
  • 2
    use class instead of id for tabBtn Commented Apr 6, 2016 at 14:18

2 Answers 2

3

Using the same id on multiple elements is invalid, use a class instead:

    $('.tabBtn').on("click", function() {
      if ($(this).parent('.job').css('max-height') == '65px') {
        $(this).parent(".job").addClass('open');
      } else {
        $(this).parent(".job").removeClass('open');
      }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="hi">
  <a class="tabBtn" href="javascript:void(0)">Hello</a>
</div>
<div class="hi">
  <a class="tabBtn" href="javascript:void(0)">Hello2</a>
</div>
<div class="hi">
  <a class="tabBtn" href="javascript:void(0)">Hello3</a>
</div>

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

Comments

1

first thing first id attribute should be ONCE per page

having say that all you need to do is work on the on function from the document and in a selecter way like so

$(document).on("click",".tabBtn",function(){
  //do work
})

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.