0

what is problem with this script? i just want to show checkbox status in innerHTML that show "yes" after click on button, if it is checked, otherwise it shown "no".

<html>
<body>
<p id="demo"></p>

<input id="chkbox" type="checkbox" name="Terms" value="agree" ><br>
<input type="button" value="button" onClick="myFunction()" >

<script>
function myFunction() {

 var box = document.getElementById("chkbox");
 if(checkbox.checked)
  {
  var checked.value = "yes";
    var txt = checked.value;
 document.getElementById("demo").innerHTML = txt;
   }
   else if(checkbox.unchecked)
{
  var unchecked.value = "no";
  var txt = unchecked.value;
 document.getElementById("demo").innerHTML = txt;
}

   }
 </script>
 </body>

4
  • There's no checkbox.unchecked. If the checkbox.checked isn't true, then it's not checked -- you don't have to look at another property. Commented Dec 31, 2013 at 11:41
  • I've never seen unchecked property for checkboxes! Commented Dec 31, 2013 at 11:41
  • what is this var checked.value = "yes";? Your var box is used nowhere. too much mistakes... Commented Dec 31, 2013 at 11:41
  • You need to define like var unchecked = { value: "no" } Commented Dec 31, 2013 at 11:43

5 Answers 5

1
function myFunction() {
    var box = document.getElementById("chkbox");
    if(box.checked)
    {
        document.getElementById("demo").innerHTML = 'yes'
    }
    else
    {
        document.getElementById("demo").innerHTML = 'no';
    }
}

The problems in your code were:

  • You set the variable box, but then used checkbox.checked instead of box.checked.
  • You looked for checkbox.unchecked. There's no such property; if .checked isn't true, then the box is unchecked.
  • You tried to declare variables checked.value and unchecked.value. Variable names can't contain ., that's used for specifying object properties when accessing a variable whose value is an object.
Sign up to request clarification or add additional context in comments.

Comments

1

There are multiple problems.

  1. There is no variable with the name checkbox
  2. The syntax var checked.value = "yes"; is invalid

Try

<input type="button" value="button" onClick="myFunction()">

then

function myFunction() {
    var box = document.getElementById("chkbox");
    box.value = box.checked ? "yes" : 'no';
    document.getElementById("demo").innerHTML = box.value;
}

Demo: Fiddle


Since jQuery tag is used include jQuery library in the page then

<input type="button" value="button" id="button">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

and

//dom ready handler
jQuery(function ($) {
    //cache the elements for future use
    var $chk = $('#chkbox'), // id-selector
        $demo = $('#demo');
    //click event handler
    $('#button').click(function () {
        //use is() and :checked-selector to check whether the checkbox is checked and use .text() to set the display text of the p element
        $demo.text($chk.is(':checked') ? 'yes' : 'no')
    })
})

Demo: Fiddle

1 Comment

@Barmar sorry might be something happened when I copied it to fiddle
0

try:

  • You assigned element to box not to checkbox
  • There is nothing like checkbox.unchecked
  • var name checked.value is not correct format.

Here is code:

function myFunction() {
   var box = document.getElementById("chkbox");
   if (box.checked) {
    document.getElementById("demo").innerHTML = "yes";
   } else {
    document.getElementById("demo").innerHTML = "no";
   }
}

Here is working Demo

Comments

0

Check this one

function myFunction() {
      if(document.getElementById('chkbox').checked) {
        alert("checked");
    } else {
       alert("not checked")
    }
    }

1 Comment

I don't want javascript alert bro, i just want to show the status in innerHTML tag.
0

try something like this,Shorthand

function myFunction(){
    var box = document.getElementById("chkbox").checked;
    document.getElementById("demo").innerHTML = box ? 'yes' : 'no';
}

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.