EzDevInfo.com

checkbox interview questions

Top checkbox frequently asked interview questions

How to create an HTML checkbox with a clickable label

How can I create an HTML checkbox with a label that is clickable (this means that clicking on the label turns the checkbox on/off)?


Source: (StackOverflow)

How to style checkbox using CSS?

I am trying to style checkbox using the following:

<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />

But, it is still displaying the default checkbox. How to do it?


Source: (StackOverflow)

Advertisements

Tri-state Check box in HTML?

There is no way to have a tri-state check button (yes, no, null) in HTML, right?

Are there any simple tricks or work-arounds without having to render the whole thing by oneself?


Source: (StackOverflow)

Check if checkbox is checked with jQuery

How can I check if a checkbox in a checkbox array is checked using the id of the checkbox array?

I am using the following code, but it always returns the count of checked checkboxes regardless of id.

function isCheckedById(id) {
  alert(id);
  var checked = $("input[@id=" + id + "]:checked").length;
  alert(checked);

  if (checked == 0) {
    return false;
  } else {
    return true;
  }
}

Source: (StackOverflow)

Check checkbox checked property

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is checked, then I need to show a textbox to enter age, else hide the textbox.

But the following code returns false by default:

if($('#isAgeSelected').attr('checked')) {
    $("#txtAge").show();
} else {
    $("#txtAge").hide();
}

How do I successfully query the checked property?


Source: (StackOverflow)

Checking a checkbox with jQuery?

I'd like to do something like this to tick a checkbox using jQuery:

$(".myCheckBox").checked(true);

or

$(".myCheckBox").selected(true);

Does such a thing exist?


Source: (StackOverflow)

Custom checkbox image android

Is there an easy way to use a custom image for a checkbox? I'm looking to duplicate the "starred" behavior of gmail. So I want to have a checkbox that, when checked, is a filled in star. And when unchecked is an empty star. Do I have to use an imageview and do my own logic myself?


Source: (StackOverflow)

jQuery if checkbox is checked

I have a function below that I want to only trigger when a checkbox in the same tr is checked. Please tell me what I am doing wrong, the usual methods are not working. Thanks

JS

$(".add_menu_item_table").live('click', function() {
var value_td = $(this).parents('tr').find('td.td_name').text();
if ($('input.checkbox_check').attr(':checked')); {
    var newDiv = $('<div class="div_menu_button"></div>');
    var showDiv = $('<div id="show'+ "0" + numShow++ +'" class="menu_button_info hidden"></div>');
    var toggleTrigger = $('<a id="toggleshow'+ "0" + numToggle++ +'" data-target="#show'+ "0" + numTarget++ +'" class="toggle_trigger actions">&nbsp;</a><div style="padding:5px"></div>');
    var menuForm = $('<form id="menu_edit_form'+ "0" + numForm++ +'" class="menu_creation_form"></form>');
    $('#created_buttons_list').append(
        newDiv.text(value_td));
        newDiv.wrap("<li></li>");
        newDiv.append(toggleTrigger);
        newDiv.append(showDiv);
        showDiv.append(menuForm);
        menuForm.html('<label for="navigation_label">Navigation Label</label><input id="navigation_label'+ "0" + numLabelone++ +'" type="text" placeholder="Navigation Label" name="navigation_label"><label for="attribute">Attribute</label><input id="attribute'+ "0" + numLabeltwo++ +'" type="text" type="text" placeholder="Attribute" name="attribute"><label for="url">URL</label><input id="url'+ "0" + numLabelthree++ +'" type="text" type="text" placeholder="URL" name="url"><input type="button" value="Remove" class="button_link remove_button"> <input type="reset" value="Cancel" class="button_link">');
}
});

var numToggle = 0;
var numShow = 0;
var numTarget = 0;
var numForm = 0;
var numLabelone = 0;
var numLabeltwo = 0;
var numLabelthree = 0;

HTML

<table width="316px" border="0" cellspacing="0" cellpadding="0" id="table-data">
                  <tbody>
                  <tr>
                    <td width="20px"><input type="checkbox" style="width:20px;" value="1" name="checkbox"></td>
                    <td width="200px"><a rel='nofollow' href="/admin/feedbackmanager/sortby/2/sortdesc/0">Page Name</a></td>
                    <td width="20px"><a rel='nofollow' href="/admin/feedbackmanager/sortby/3/sortdesc/0">Add</a></td>
                  </tr>
                  <tr>
                    <td><input type="checkbox" style="width:20px;" value="1" name="checkbox" class="checkbox_check"></td>
                    <td class="td_name">Timeplot</td>
                    <td><input class="add_menu_item_table" name="add_menu_item" value="Add" type="button"></td>
                  </tr>
                  <tr>
                    <td><input type="checkbox" style="width:20px;" value="1" name="checkbox" class="checkbox_check"></td>
                    <td class="td_name">Operations Manuals</td>
                    <td><input class="add_menu_item_table" name="add_menu_item" value="Add" type="button"></td>
                  </tr>
                  <tr>
                    <td><input type="checkbox" style="width:20px;" value="1" name="checkbox" class="checkbox_check"></td>
                    <td class="td_name">Company Structure</td>
                    <td><input class="add_menu_item_table" name="add_menu_item" value="Add" type="button"></td>
                  </tr>
                  <tr>
                    <td><input type="checkbox" style="width:20px;" value="1" name="checkbox" class="checkbox_check"></td>
                    <td class="td_name">CMS Report</td>
                    <td><input class="add_menu_item_table" name="add_menu_item" value="Add" type="button"></td>
                  </tr>
                  <tr>
                    <td><input type="checkbox" style="width:20px;" value="1" name="checkbox" class="checkbox_check"></td>
                    <td class="td_name">Test Document</td>
                    <td><input class="add_menu_item_table" name="add_menu_item" value="Add" type="button"></td>
                  </tr>
                  <tr>
                    <td><input type="checkbox" style="width:20px;" value="1" name="checkbox" class="checkbox_check"></td>
                    <td class="td_name">Test CMS page</td>
                    <td><input class="add_menu_item_table" name="add_menu_item" value="Add" type="button"></td>
                  </tr>
                  </tbody>
                  </table>

Source: (StackOverflow)

How to read if a checkbox is checked in PHP?

How to read if a checkbox is checked in PHP?


Source: (StackOverflow)

Post the checkboxes that are unchecked

I've got a load of checkboxes that are by default checked. My users will probably uncheck a few of the checkboxes (if any) and leave the rest of them checked. Is there any way to get the checkboxes that are NOT checked in a form post, rather than the ones that are checked?


Source: (StackOverflow)

Get checkbox value in jQuery

How can I get a checkbox's value in jQuery?


Source: (StackOverflow)

jquery set checkbox checked

I already tried all the possible ways, but I still didn't get it working. I have a modal window with a checkbox I want that when the modal opens, the checkbox check or uncheck should be based on a database value. (I have that already working with others form fields.) I started trying to get it checked but it didn't work.

My html div:

<div id="fModal" class="modal" >
    ...
    <div class="row-form">
        <div class="span12">
            <span class="top title">Estado</span>

          <input type="checkbox"  id="estado_cat" class="ibtn">
       </div>
    </div>             
</div>

and the jquery:

$("#estado_cat").prop( "checked", true );

I also tried with attr, and others seen here in the forums, but none seem to work. Can someone point me the right way?

EDIT: ok, I'm really missing something here... I can check/uncheck using code if the check box is in the page, but is it's in the modal window, I can't. I tried dozens of different ways...

I have a link that's supposed to open the modal:

and jquery to "listen" the click and execute some operations like filling some text boxes with data coming from database. Everything works like I want but the problem is that I can't set checkbox checked/unchecked using code. help please!

$(function() {
 $(".editButton").click(function(){
       var id = $(this).data('id'); 
       $.ajax({
          type: "POST",
          url: "process.php",
          dataType:"json",
          data: { id: id, op: "edit" },
        }).done(function( data ) {
//the next two lines work fine, i.e., it grabs the value from database and fills the textboxes
          $("#nome_categoria").val( data['nome_categoria'] );
          $("#descricao_categoria").val( data['descricao_categoria'] );
//then I tried to set the checkbox checked (because its unchecked by default) and it does not work
           $("#estado_cat").prop("checked", true);
        $('#fModal').modal('show');
});
    evt.preventDefault();
    return false;
    });     
});

Source: (StackOverflow)

How to retrieve checkboxes values in jQuery

How to use jQuery to get the checked checkboxes values, and put it into a textarea immediately?

Just like this code:

<html>
  <head>
  </head>

  <body>
    <div id="c_b">
      <input type="checkbox" value="one_name" checked>
      <input type="checkbox" value="one_name1">
      <input type="checkbox" value="one_name2">
    </div>  

    <textarea id="t"></textarea>
  </body>
</html>

If the id="c_d" is updated by Ajax, the below of altCognito's code doesn't work. Is there any good solution?


Source: (StackOverflow)

What's the proper value for a checked attribute of an HTML checkbox?

We all know how to form a checkbox input in HTML:

<input name="checkbox_name" id="checkbox_id" type="checkbox">

What I don't know -- what's the technically correct value for a checked checkbox? I've seen these all work:

<input name="checkbox_name" id="checkbox_id" type="checkbox" checked>
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="on">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="yes">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="checked">
<input name="checkbox_name" id="checkbox_id" type="checkbox" checked="true">

Is the answer that it doesn't matter? I see no evidence for the answer marked as correct here from the spec itself:

Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful. Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property. The INPUT element is used to create a checkbox control.

What would a spec writer say is the correct answer? Please provide evidence-based answers.


Source: (StackOverflow)

How to uncheck checkbox using jQuery Uniform library

I have a problem with unchecking a checkbox. Have a look at my jsFiddle, where I am attempting:

   $("#check2").attr("checked", true);

I use uniform for styling the checkbox and it simply does not work to check/uncheck the checkbox.

Any ideas?


Source: (StackOverflow)