1

I want to show div if checkbox is checked. If not, I want to hide it. Here is my code, but it doesn't work.

$(".formularz_zgloszeniowy .firma input[type='checkbox']").change(function() {
    if(this.checked) {
        $(".formularz_zgloszeniowy .formularz_firma").css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​
    } 
    else 
    {
      $(".formularz_zgloszeniowy .formularz_firma").css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'none');​​​​​​
    }
}); 

I'm getting this error:

Uncaught SyntaxError: missing ) after argument list

EDIT: Here is full code in head tag:

<script type="text/javascript">
jQuery(document).ready(function($){

$(".formularz_zgloszeniowy .firma input[type='checkbox']").change(function() {
    if($(this).is(":checked")) {
        $(".formularz_zgloszeniowy .formularz_firma").css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​
    } 
    else 
    {
      $(".formularz_zgloszeniowy .formularz_firma").css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'none');​​​​​​
    }
});

});
</script>
9
  • 4
    just use : if($(this).is(":checked")) Commented Nov 23, 2015 at 19:17
  • 1
    That code should not throw such error. The culprit must be something else. Commented Nov 23, 2015 at 19:19
  • 3
    There's a ton of non-printable characters in the example, probably in your real code too? Commented Nov 23, 2015 at 19:20
  • 1
    @DinoMyte That's just less efficient. Commented Nov 23, 2015 at 19:20
  • 3
    @gkopowski Just check the fiddle I've linked. You see the red dots? They are \u200b characters breaking your code. Commented Nov 23, 2015 at 19:26

1 Answer 1

1

You have some invisible characters in your code that are causing the problem. Here is your code working perfectly without those characters:

$(".formularz_zgloszeniowy .firma input[type='checkbox']").change(function(){
  if(this.checked) {
    $(".formularz_zgloszeniowy .formularz_firma").css('display','block');
  } 
  else {
    $(".formularz_zgloszeniowy .formularz_firma").css('display','none');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="formularz_zgloszeniowy">
  <div class="firma">
    <input type="checkbox" value="test" /> Test checkbox
  </div>
  <div class="formularz_firma">Test div</div>
</div>

And here is the code copied from your question with those invisible characters, so it is not working:

$(".formularz_zgloszeniowy .firma input[type='checkbox']").change(function() {
    if(this.checked) {
        $(".formularz_zgloszeniowy .formularz_firma").css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​
    } 
    else 
    {
      $(".formularz_zgloszeniowy .formularz_firma").css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'none');​​​​​​
    }
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="formularz_zgloszeniowy">
  <div class="firma">
    <input type="checkbox" value="test" /> Test checkbox
  </div>
  <div class="formularz_firma">Test div</div>
</div>

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

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.