1

I'm trying to hide element when the checkbox is clicked.

Somehow it didn't work -

html

<input type="checkbox"  onchange="showAtidit()">
<p id="hidethis">
hide me
</p>

js

function showAtidit($)
{
    if ($(this).is(':checked')) 
    {
        alert("not hide");
        document.getElementById( "hidethis" ).style.display = 'block';
    }
    else
    {
        alert("hide");
        document.getElementById( "hidethis" ).style.display = 'none';
    }
};

http://jsfiddle.net/wv97mpcp/108/

What's wrong?

Thanks!

4
  • You are redifining $ in showAtidit($) Commented Nov 3, 2016 at 9:26
  • see comment below. BTW - why the $ sign is problem? because it waits for parameter and didnt get one? Commented Nov 3, 2016 at 9:28
  • Because that is used by jQuery. Now you have redefined it as = undefined, so no longer represents the jQuery library Commented Nov 3, 2016 at 9:31
  • Got it! Thanks! :) Commented Nov 3, 2016 at 9:35

5 Answers 5

4

Since you're using jquery just use toggle() :

function showAtidit()
{
   $('#hidethis').toggle();
}

Hope this helps.

function showAtidit()
{
  $('#hidethis').toggle();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox"  onchange="showAtidit()">
<p id="hidethis">
hide me
</p>

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

2 Comments

No @JohnJs you should use jquery object $('#hidethis') instead of js object document.getElementById( "hidethis" ) so you could use the jQuery function toggle(), else you'll get .toggle is not a function error..
can't use .toggle in javascript :)
2

remove the $ in your function parameters and the problem will be fixed :)

function showAtidit()
    {
        if ($(this).is(':checked')) 
        {
            alert("not hide");
            document.getElementById( "hidethis" ).style.display = 'block';
        }
        else
        {
            alert("hide");
            document.getElementById( "hidethis" ).style.display = 'none';
        }
    };

could also do it like this:

function showAtidit()
    {
        if(document.getElementById('checkbox').checked) {
          $("#hidethis").hide();
        } 
        else {
          $("#hidethis").show();
        }
    };

(have to add id to checkbox tho)

Comments

1

Since you are using jQuery, toggle is the way to go, Here I am testing the checked:

$(function(){
  $("#toggleP").on("click",function() {
    $("#hidethis").toggle(!this.checked);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" id="toggleP">
<p id="hidethis">
hide me
</p>

If you want to look at the state when loading the page:

$(function(){
  $("#toggleP").on("change",function() {
    $("#hidethis").toggle(!this.checked);
  }).change();
});

Comments

0

Please check this out. I hope it will help

$('#sample').on("change",showAtidit)
function showAtidit()
	{
		if ($(this).is(':checked')) 
		{
			alert("not hide");
			document.getElementById( "hidethis" ).style.display = 'block';
		}
		else
		{
			alert("hide");
      
			document.getElementById( "hidethis" ).style.display = 'none';
		}
	};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	
	<input id="sample" type="checkbox">
<p id="hidethis">
hide me
</p>

Comments

0

Using $(this).toggle() is risky as you can guarentee the initial state of checkbox which may cause the checkbox not work as intended.

The actually problem with your code is the $ in the function variable.

You can see this is the issue by looking in the console on the jsfiddle you posted by pressing f12.

Here is a revised JSFiddle that will work.

http://jsfiddle.net/wv97mpcp/112/

1 Comment

You can test the state too

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.